blob: be2cd14a72013113c0e04d1e014603d5a3cc1e02 [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 Rossum21142a01999-01-08 21:05:37 +0000404posix_int(args, func)
405 PyObject *args;
406 int (*func) Py_FPROTO((int));
407{
408 int fd;
409 int res;
410 if (!PyArg_Parse(args, "i", &fd))
411 return NULL;
412 Py_BEGIN_ALLOW_THREADS
413 res = (*func)(fd);
414 Py_END_ALLOW_THREADS
415 if (res < 0)
416 return posix_error();
417 Py_INCREF(Py_None);
418 return Py_None;
419}
420
421
422static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000423posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000424 PyObject *args;
425 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000427 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000428 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000429 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000431 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000432 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000433 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000434 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000435 return posix_error_with_filename(path1);
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_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000442 PyObject *args;
443 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000445 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000446 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000447 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000449 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000450 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000451 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000452 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000453 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000454 return posix_error();
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 Rossum85a5fbb1990-10-14 12:07:46 +0000460posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000461 PyObject *args;
462 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000463{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000464 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000465 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000466 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000469 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000470 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000471 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58: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 Rossum85a5fbb1990-10-14 12:07:46 +0000476}
477
Barry Warsaw53699e91996-12-10 23:23:01 +0000478static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000479posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000480 PyObject *args;
481 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000482{
483 char *path;
484 int i,i2;
485 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000486 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000487 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000488 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000489 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000490 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000491 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000492 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000493 Py_INCREF(Py_None);
494 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000495}
496
Barry Warsaw53699e91996-12-10 23:23:01 +0000497static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000499 PyObject *self;
500 PyObject *args;
501 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502{
503 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000504 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000505 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000506 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000508 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000509 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000510 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000511 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000512 return posix_error_with_filename(path);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000513#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000514 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000515 (long)st.st_mode,
516 (long)st.st_ino,
517 (long)st.st_dev,
518 (long)st.st_nlink,
519 (long)st.st_uid,
520 (long)st.st_gid,
521 (long)st.st_size,
522 (long)st.st_atime,
523 (long)st.st_mtime,
524 (long)st.st_ctime);
525#else
526 return Py_BuildValue("(lLllllLlll)",
527 (long)st.st_mode,
528 (LONG_LONG)st.st_ino,
529 (long)st.st_dev,
530 (long)st.st_nlink,
531 (long)st.st_uid,
532 (long)st.st_gid,
533 (LONG_LONG)st.st_size,
534 (long)st.st_atime,
535 (long)st.st_mtime,
536 (long)st.st_ctime);
537#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538}
539
540
541/* POSIX methods */
542
Guido van Rossum94f6f721999-01-06 18:42:14 +0000543static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000544"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000545Test for access to a file.";
546
547static PyObject *
548posix_access(self, args)
549 PyObject *self;
550 PyObject *args;
551{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000552 char *path;
553 int mode;
554 int res;
555
556 if (!PyArg_Parse(args, "(si)", &path, &mode))
557 return NULL;
558 Py_BEGIN_ALLOW_THREADS
559 res = access(path, mode);
560 Py_END_ALLOW_THREADS
561 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000562}
563
564static char posix_ttyname__doc__[] =
565"ttyname(fd, mode) -> String\n\
566Return the name of the terminal device connected to 'fd'.";
567
568static PyObject *
569posix_ttyname(self, args)
570 PyObject *self;
571 PyObject *args;
572{
573 PyObject *file;
574 int id;
575 char *ret;
576
Guido van Rossum94f6f721999-01-06 18:42:14 +0000577 if (!PyArg_Parse(args, "i", &id))
578 return NULL;
579
Guido van Rossum94f6f721999-01-06 18:42:14 +0000580 ret = ttyname(id);
581 if (ret == NULL)
582 return(posix_error());
583 return(PyString_FromString(ret));
584}
585
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000586static char posix_chdir__doc__[] =
587"chdir(path) -> None\n\
588Change the current working directory to the specified path.";
589
Barry Warsaw53699e91996-12-10 23:23:01 +0000590static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000591posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000592 PyObject *self;
593 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595 return posix_1str(args, chdir);
596}
597
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000598
599static char posix_chmod__doc__[] =
600"chmod(path, mode) -> None\n\
601Change the access permissions of a file.";
602
Barry Warsaw53699e91996-12-10 23:23:01 +0000603static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000605 PyObject *self;
606 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000608 return posix_strint(args, chmod);
609}
610
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000611
Guido van Rossum21142a01999-01-08 21:05:37 +0000612#ifdef HAVE_FSYNC
613static char posix_fsync__doc__[] =
614"fsync(fildes) -> None\n\
615force write of file with filedescriptor to disk.";
616
617static PyObject *
618posix_fsync(self, args)
619 PyObject *self;
620 PyObject *args;
621{
Guido van Rossum21142a01999-01-08 21:05:37 +0000622 return posix_int(args, fsync);
623}
624#endif /* HAVE_FSYNC */
625
626#ifdef HAVE_FDATASYNC
627static char posix_fdatasync__doc__[] =
628"fdatasync(fildes) -> None\n\
629force write of file with filedescriptor to disk.\n\
630 does not force update of metadata.";
631
Guido van Rossum5d00b6d1999-01-08 21:28:05 +0000632extern int fdatasync(int); /* Prototype just in case */
633
Guido van Rossum21142a01999-01-08 21:05:37 +0000634static PyObject *
635posix_fdatasync(self, args)
636 PyObject *self;
637 PyObject *args;
638{
Guido van Rossum21142a01999-01-08 21:05:37 +0000639 return posix_int(args, fdatasync);
640}
641#endif /* HAVE_FDATASYNC */
642
643
Guido van Rossumb6775db1994-08-01 11:34:53 +0000644#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000645static char posix_chown__doc__[] =
646"chown(path, uid, gid) -> None\n\
647Change the owner and group id of path to the numeric uid and gid.";
648
Barry Warsaw53699e91996-12-10 23:23:01 +0000649static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000650posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000651 PyObject *self;
652 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000653{
654 return posix_strintint(args, chown);
655}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000656#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000657
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000658
Guido van Rossum36bc6801995-06-14 22:54:23 +0000659#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000660static char posix_getcwd__doc__[] =
661"getcwd() -> path\n\
662Return a string representing the current working directory.";
663
Barry Warsaw53699e91996-12-10 23:23:01 +0000664static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000665posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000666 PyObject *self;
667 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000668{
669 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000670 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000671 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000673 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000674 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000675 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000676 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000677 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000678 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000679}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000680#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000682
Guido van Rossumb6775db1994-08-01 11:34:53 +0000683#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000684static char posix_link__doc__[] =
685"link(src, dst) -> None\n\
686Create a hard link to a file.";
687
Barry Warsaw53699e91996-12-10 23:23:01 +0000688static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000689posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000690 PyObject *self;
691 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000692{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000693 return posix_2str(args, link);
694}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000695#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000697
698static char posix_listdir__doc__[] =
699"listdir(path) -> list_of_strings\n\
700Return a list containing the names of the entries in the directory.\n\
701\n\
702 path: path of directory to list\n\
703\n\
704The list is in arbitrary order. It does not include the special\n\
705entries '.' and '..' even if they are present in the directory.";
706
Barry Warsaw53699e91996-12-10 23:23:01 +0000707static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000708posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000709 PyObject *self;
710 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000711{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000712 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000713 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000714#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000715
Guido van Rossumb6775db1994-08-01 11:34:53 +0000716 char *name;
717 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000718 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000719 HANDLE hFindFile;
720 WIN32_FIND_DATA FileData;
721 char namebuf[MAX_PATH+5];
722
Guido van Rossum7e488981998-10-08 02:25:24 +0000723 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000724 return NULL;
725 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000726 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000727 return NULL;
728 }
729 strcpy(namebuf, name);
730 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
731 namebuf[len++] = '/';
732 strcpy(namebuf + len, "*.*");
733
Barry Warsaw53699e91996-12-10 23:23:01 +0000734 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000735 return NULL;
736
737 hFindFile = FindFirstFile(namebuf, &FileData);
738 if (hFindFile == INVALID_HANDLE_VALUE) {
739 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000740 if (errno == ERROR_FILE_NOT_FOUND)
741 return PyList_New(0);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000742 return posix_error();
743 }
744 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000745 if (FileData.cFileName[0] == '.' &&
746 (FileData.cFileName[1] == '\0' ||
747 FileData.cFileName[1] == '.' &&
748 FileData.cFileName[2] == '\0'))
749 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000750 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000751 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000752 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000753 d = NULL;
754 break;
755 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000756 if (PyList_Append(d, v) != 0) {
757 Py_DECREF(v);
758 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000759 d = NULL;
760 break;
761 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000762 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000763 } while (FindNextFile(hFindFile, &FileData) == TRUE);
764
765 if (FindClose(hFindFile) == FALSE) {
766 errno = GetLastError();
767 return posix_error();
768 }
769
770 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000771
Guido van Rossum8d665e61996-06-26 18:22:49 +0000772#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000773#ifdef _MSC_VER /* 16-bit Windows */
774
775#ifndef MAX_PATH
776#define MAX_PATH 250
777#endif
778 char *name, *pt;
779 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000780 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000781 char namebuf[MAX_PATH+5];
782 struct _find_t ep;
783
Guido van Rossum7e488981998-10-08 02:25:24 +0000784 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000785 return NULL;
786 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000787 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000788 return NULL;
789 }
790 strcpy(namebuf, name);
791 for (pt = namebuf; *pt; pt++)
792 if (*pt == '/')
793 *pt = '\\';
794 if (namebuf[len-1] != '\\')
795 namebuf[len++] = '\\';
796 strcpy(namebuf + len, "*.*");
797
Barry Warsaw53699e91996-12-10 23:23:01 +0000798 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000799 return NULL;
800
801 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000802 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
803 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000804 errno = ENOENT;
805 return posix_error();
806 }
807 do {
808 if (ep.name[0] == '.' &&
809 (ep.name[1] == '\0' ||
810 ep.name[1] == '.' &&
811 ep.name[2] == '\0'))
812 continue;
813 strcpy(namebuf, ep.name);
814 for (pt = namebuf; *pt; pt++)
815 if (isupper(*pt))
816 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000817 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000818 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000819 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000820 d = NULL;
821 break;
822 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000823 if (PyList_Append(d, v) != 0) {
824 Py_DECREF(v);
825 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000826 d = NULL;
827 break;
828 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000829 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000830 } while (_dos_findnext(&ep) == 0);
831
832 return d;
833
834#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000835#if defined(PYOS_OS2)
836
837#ifndef MAX_PATH
838#define MAX_PATH CCHMAXPATH
839#endif
840 char *name, *pt;
841 int len;
842 PyObject *d, *v;
843 char namebuf[MAX_PATH+5];
844 HDIR hdir = 1;
845 ULONG srchcnt = 1;
846 FILEFINDBUF3 ep;
847 APIRET rc;
848
Guido van Rossum7e488981998-10-08 02:25:24 +0000849 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000850 return NULL;
851 if (len >= MAX_PATH) {
852 PyErr_SetString(PyExc_ValueError, "path too long");
853 return NULL;
854 }
855 strcpy(namebuf, name);
856 for (pt = namebuf; *pt; pt++)
857 if (*pt == '/')
858 *pt = '\\';
859 if (namebuf[len-1] != '\\')
860 namebuf[len++] = '\\';
861 strcpy(namebuf + len, "*.*");
862
863 if ((d = PyList_New(0)) == NULL)
864 return NULL;
865
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000866 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
867 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000868 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000869 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
870 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
871 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000872
873 if (rc != NO_ERROR) {
874 errno = ENOENT;
875 return posix_error();
876 }
877
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000878 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000879 do {
880 if (ep.achName[0] == '.'
881 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000882 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000883
884 strcpy(namebuf, ep.achName);
885
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000886 /* Leave Case of Name Alone -- In Native Form */
887 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000888
889 v = PyString_FromString(namebuf);
890 if (v == NULL) {
891 Py_DECREF(d);
892 d = NULL;
893 break;
894 }
895 if (PyList_Append(d, v) != 0) {
896 Py_DECREF(v);
897 Py_DECREF(d);
898 d = NULL;
899 break;
900 }
901 Py_DECREF(v);
902 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
903 }
904
905 return d;
906#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000907
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000908 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000909 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000910 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000911 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000912 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000913 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000914 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000915 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000916 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000917 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000918 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000919 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000920 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000921 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000922 return NULL;
923 }
924 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000925 if (ep->d_name[0] == '.' &&
926 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000927 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000928 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000929 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000930 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000931 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000932 d = NULL;
933 break;
934 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000935 if (PyList_Append(d, v) != 0) {
936 Py_DECREF(v);
937 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000938 d = NULL;
939 break;
940 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000941 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000942 }
943 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000944 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000945
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000946 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000947
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000948#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000949#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000950#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000951}
952
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000953static char posix_mkdir__doc__[] =
954"mkdir(path [, mode=0777]) -> None\n\
955Create a directory.";
956
Barry Warsaw53699e91996-12-10 23:23:01 +0000957static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000958posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000959 PyObject *self;
960 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000961{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000962 int res;
963 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000964 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000965 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000966 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000967 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000968#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000969 res = mkdir(path);
970#else
Guido van Rossumb0824db1996-02-25 04:50:32 +0000971 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000972#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000973 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +0000974 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000975 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000976 Py_INCREF(Py_None);
977 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000978}
979
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000980
Guido van Rossumb6775db1994-08-01 11:34:53 +0000981#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000982static char posix_nice__doc__[] =
983"nice(inc) -> new_priority\n\
984Decrease the priority of process and return new priority.";
985
Barry Warsaw53699e91996-12-10 23:23:01 +0000986static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +0000987posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000988 PyObject *self;
989 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +0000990{
991 int increment, value;
992
Barry Warsaw53699e91996-12-10 23:23:01 +0000993 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +0000994 return NULL;
995 value = nice(increment);
996 if (value == -1)
997 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000998 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +0000999}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001000#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001001
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001002
1003static char posix_rename__doc__[] =
1004"rename(old, new) -> None\n\
1005Rename a file or directory.";
1006
Barry Warsaw53699e91996-12-10 23:23:01 +00001007static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001008posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001009 PyObject *self;
1010 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001011{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001012 return posix_2str(args, rename);
1013}
1014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001015
1016static char posix_rmdir__doc__[] =
1017"rmdir(path) -> None\n\
1018Remove a directory.";
1019
Barry Warsaw53699e91996-12-10 23:23:01 +00001020static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001021posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001022 PyObject *self;
1023 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001024{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001025 return posix_1str(args, rmdir);
1026}
1027
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001028
1029static char posix_stat__doc__[] =
1030"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1031Perform a stat system call on the given path.";
1032
Barry Warsaw53699e91996-12-10 23:23:01 +00001033static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001034posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001035 PyObject *self;
1036 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001037{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001038 return posix_do_stat(self, args, stat);
1039}
1040
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001041
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001042#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001043static char posix_system__doc__[] =
1044"system(command) -> exit_status\n\
1045Execute the command (a string) in a subshell.";
1046
Barry Warsaw53699e91996-12-10 23:23:01 +00001047static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001048posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001049 PyObject *self;
1050 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001051{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001052 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001053 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001054 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001055 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001056 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001057 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001058 Py_END_ALLOW_THREADS
1059 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001060}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001061#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001062
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001063
1064static char posix_umask__doc__[] =
1065"umask(new_mask) -> old_mask\n\
1066Set the current numeric umask and return the previous umask.";
1067
Barry Warsaw53699e91996-12-10 23:23:01 +00001068static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001069posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001070 PyObject *self;
1071 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001072{
1073 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +00001074 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001075 return NULL;
1076 i = umask(i);
1077 if (i < 0)
1078 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001079 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001080}
1081
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001082
1083static char posix_unlink__doc__[] =
1084"unlink(path) -> None\n\
1085Remove a file (same as remove(path)).";
1086
1087static char posix_remove__doc__[] =
1088"remove(path) -> None\n\
1089Remove a file (same as unlink(path)).";
1090
Barry Warsaw53699e91996-12-10 23:23:01 +00001091static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001092posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001093 PyObject *self;
1094 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001095{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001096 return posix_1str(args, unlink);
1097}
1098
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001099
Guido van Rossumb6775db1994-08-01 11:34:53 +00001100#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001101static char posix_uname__doc__[] =
1102"uname() -> (sysname, nodename, release, version, machine)\n\
1103Return a tuple identifying the current operating system.";
1104
Barry Warsaw53699e91996-12-10 23:23:01 +00001105static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001106posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001107 PyObject *self;
1108 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001109{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001110 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001111 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001112 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001113 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001114 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001115 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001116 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001117 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001118 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001119 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001120 u.sysname,
1121 u.nodename,
1122 u.release,
1123 u.version,
1124 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001125}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001126#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001127
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001128
1129static char posix_utime__doc__[] =
1130"utime(path, (atime, utime)) -> None\n\
1131Set the access and modified time of the file to the given values.";
1132
Barry Warsaw53699e91996-12-10 23:23:01 +00001133static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001134posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001135 PyObject *self;
1136 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001137{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001138 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001139 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001140 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001141
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001142/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001143#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001144 struct utimbuf buf;
1145#define ATIME buf.actime
1146#define MTIME buf.modtime
1147#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001148#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001149 time_t buf[2];
1150#define ATIME buf[0]
1151#define MTIME buf[1]
1152#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001153#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001154
Barry Warsaw53699e91996-12-10 23:23:01 +00001155 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001156 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001157 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001158 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001159 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001160 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001161 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001162 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001163 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001164 Py_INCREF(Py_None);
1165 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001166#undef UTIME_ARG
1167#undef ATIME
1168#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001169}
1170
Guido van Rossum85e3b011991-06-03 12:42:10 +00001171
Guido van Rossum3b066191991-06-04 19:40:25 +00001172/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001173
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001174static char posix__exit__doc__[] =
1175"_exit(status)\n\
1176Exit to the system with specified status, without normal exit processing.";
1177
Barry Warsaw53699e91996-12-10 23:23:01 +00001178static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001179posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001180 PyObject *self;
1181 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001182{
1183 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001184 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001185 return NULL;
1186 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001187 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001188}
1189
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001190
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001191#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001192static char posix_execv__doc__[] =
1193"execv(path, args)\n\
1194Execute an executable path with arguments, replacing current process.\n\
1195\n\
1196 path: path of executable file\n\
1197 args: tuple or list of strings";
1198
Barry Warsaw53699e91996-12-10 23:23:01 +00001199static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001200posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001201 PyObject *self;
1202 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001203{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001204 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001205 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001206 char **argvlist;
1207 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001208 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001209
Guido van Rossum89b33251993-10-22 14:26:06 +00001210 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001211 argv is a list or tuple of strings. */
1212
Barry Warsaw53699e91996-12-10 23:23:01 +00001213 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001214 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001215 if (PyList_Check(argv)) {
1216 argc = PyList_Size(argv);
1217 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001218 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001219 else if (PyTuple_Check(argv)) {
1220 argc = PyTuple_Size(argv);
1221 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001222 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001223 else {
1224 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001225 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001226 return NULL;
1227 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001228
Barry Warsaw53699e91996-12-10 23:23:01 +00001229 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001230 if (argvlist == NULL)
1231 return NULL;
1232 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001233 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1234 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001235 goto badarg;
1236 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001237 }
1238 argvlist[argc] = NULL;
1239
Guido van Rossumb6775db1994-08-01 11:34:53 +00001240#ifdef BAD_EXEC_PROTOTYPES
1241 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001242#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001243 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001244#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001245
Guido van Rossum85e3b011991-06-03 12:42:10 +00001246 /* If we get here it's definitely an error */
1247
Barry Warsaw53699e91996-12-10 23:23:01 +00001248 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001249 return posix_error();
1250}
1251
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001252
1253static char posix_execve__doc__[] =
1254"execve(path, args, env)\n\
1255Execute a path with arguments and environment, replacing current process.\n\
1256\n\
1257 path: path of executable file\n\
1258 args: tuple or list of arguments\n\
1259 env: dictonary of strings mapping to strings";
1260
Barry Warsaw53699e91996-12-10 23:23:01 +00001261static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001262posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001263 PyObject *self;
1264 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001265{
1266 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001267 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001268 char **argvlist;
1269 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001270 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001271 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001272 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001273
1274 /* execve has three arguments: (path, argv, env), where
1275 argv is a list or tuple of strings and env is a dictionary
1276 like posix.environ. */
1277
Barry Warsaw53699e91996-12-10 23:23:01 +00001278 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001279 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001280 if (PyList_Check(argv)) {
1281 argc = PyList_Size(argv);
1282 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001283 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001284 else if (PyTuple_Check(argv)) {
1285 argc = PyTuple_Size(argv);
1286 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001287 }
1288 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001289 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001290 return NULL;
1291 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001292 if (!PyMapping_Check(env)) {
1293 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001294 return NULL;
1295 }
1296
Barry Warsaw53699e91996-12-10 23:23:01 +00001297 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001298 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001299 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001300 return NULL;
1301 }
1302 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001303 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001304 "s;argv must be list of strings",
1305 &argvlist[i]))
1306 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001307 goto fail_1;
1308 }
1309 }
1310 argvlist[argc] = NULL;
1311
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001312 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001313 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001314 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001315 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001316 goto fail_1;
1317 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001318 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001319 keys = PyMapping_Keys(env);
1320 vals = PyMapping_Values(env);
1321 if (!keys || !vals)
1322 goto fail_2;
1323
1324 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001325 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001326
1327 key = PyList_GetItem(keys, pos);
1328 val = PyList_GetItem(vals, pos);
1329 if (!key || !val)
1330 goto fail_2;
1331
Barry Warsaw53699e91996-12-10 23:23:01 +00001332 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001333 !PyArg_Parse(val, "s;non-string value in env", &v))
1334 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001335 goto fail_2;
1336 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001337
1338#if defined(PYOS_OS2)
1339 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1340 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1341#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001342 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001343 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001344 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001345 goto fail_2;
1346 }
1347 sprintf(p, "%s=%s", k, v);
1348 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001349#if defined(PYOS_OS2)
1350 }
1351#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001352 }
1353 envlist[envc] = 0;
1354
Guido van Rossumb6775db1994-08-01 11:34:53 +00001355
1356#ifdef BAD_EXEC_PROTOTYPES
1357 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001358#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001359 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001360#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001361
1362 /* If we get here it's definitely an error */
1363
1364 (void) posix_error();
1365
1366 fail_2:
1367 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001368 PyMem_DEL(envlist[envc]);
1369 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001370 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001371 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001372 Py_XDECREF(vals);
1373 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001374 return NULL;
1375}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001376#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001377
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001378
Guido van Rossumad0ee831995-03-01 10:34:45 +00001379#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001380static char posix_fork__doc__[] =
1381"fork() -> pid\n\
1382Fork a child process.\n\
1383\n\
1384Return 0 to child process and PID of child to parent process.";
1385
Barry Warsaw53699e91996-12-10 23:23:01 +00001386static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001387posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001388 PyObject *self;
1389 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001390{
1391 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001392 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001393 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001394 pid = fork();
1395 if (pid == -1)
1396 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001397 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001398 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001399}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001400#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001401
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001402
Guido van Rossumad0ee831995-03-01 10:34:45 +00001403#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001404static char posix_getegid__doc__[] =
1405"getegid() -> egid\n\
1406Return the current process's effective group id.";
1407
Barry Warsaw53699e91996-12-10 23:23:01 +00001408static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001409posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001410 PyObject *self;
1411 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001412{
Barry Warsaw53699e91996-12-10 23:23:01 +00001413 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001414 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001415 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001416}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001417#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001418
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001419
Guido van Rossumad0ee831995-03-01 10:34:45 +00001420#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001421static char posix_geteuid__doc__[] =
1422"geteuid() -> euid\n\
1423Return the current process's effective user id.";
1424
Barry Warsaw53699e91996-12-10 23:23:01 +00001425static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001426posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001427 PyObject *self;
1428 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001429{
Barry Warsaw53699e91996-12-10 23:23:01 +00001430 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001431 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001432 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001433}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001434#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001435
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001436
Guido van Rossumad0ee831995-03-01 10:34:45 +00001437#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001438static char posix_getgid__doc__[] =
1439"getgid() -> gid\n\
1440Return the current process's group id.";
1441
Barry Warsaw53699e91996-12-10 23:23:01 +00001442static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001443posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001444 PyObject *self;
1445 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001446{
Barry Warsaw53699e91996-12-10 23:23:01 +00001447 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001448 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001449 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001450}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001451#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001452
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001453
1454static char posix_getpid__doc__[] =
1455"getpid() -> pid\n\
1456Return the current process id";
1457
Barry Warsaw53699e91996-12-10 23:23:01 +00001458static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001459posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001460 PyObject *self;
1461 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001462{
Barry Warsaw53699e91996-12-10 23:23:01 +00001463 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001464 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001465 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001466}
1467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001468
Guido van Rossumb6775db1994-08-01 11:34:53 +00001469#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001470static char posix_getpgrp__doc__[] =
1471"getpgrp() -> pgrp\n\
1472Return the current process group id.";
1473
Barry Warsaw53699e91996-12-10 23:23:01 +00001474static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001475posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001476 PyObject *self;
1477 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001478{
Barry Warsaw53699e91996-12-10 23:23:01 +00001479 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001480 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001481#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001482 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001483#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001484 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001485#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001486}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001487#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001488
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001489
Guido van Rossumb6775db1994-08-01 11:34:53 +00001490#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001491static char posix_setpgrp__doc__[] =
1492"setpgrp() -> None\n\
1493Make this process a session leader.";
1494
Barry Warsaw53699e91996-12-10 23:23:01 +00001495static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001496posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001497 PyObject *self;
1498 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001499{
Barry Warsaw53699e91996-12-10 23:23:01 +00001500 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001501 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001502#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001503 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001504#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001505 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001506#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001507 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001508 Py_INCREF(Py_None);
1509 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001510}
1511
Guido van Rossumb6775db1994-08-01 11:34:53 +00001512#endif /* HAVE_SETPGRP */
1513
Guido van Rossumad0ee831995-03-01 10:34:45 +00001514#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001515static char posix_getppid__doc__[] =
1516"getppid() -> ppid\n\
1517Return the parent's process id.";
1518
Barry Warsaw53699e91996-12-10 23:23:01 +00001519static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001520posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001521 PyObject *self;
1522 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001523{
Barry Warsaw53699e91996-12-10 23:23:01 +00001524 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001525 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001526 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001527}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001528#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001529
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001530
Guido van Rossumad0ee831995-03-01 10:34:45 +00001531#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001532static char posix_getuid__doc__[] =
1533"getuid() -> uid\n\
1534Return the current process's user id.";
1535
Barry Warsaw53699e91996-12-10 23:23:01 +00001536static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001537posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001538 PyObject *self;
1539 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001540{
Barry Warsaw53699e91996-12-10 23:23:01 +00001541 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001542 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001543 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001544}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001545#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001546
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001547
Guido van Rossumad0ee831995-03-01 10:34:45 +00001548#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001549static char posix_kill__doc__[] =
1550"kill(pid, sig) -> None\n\
1551Kill a process with a signal.";
1552
Barry Warsaw53699e91996-12-10 23:23:01 +00001553static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001554posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001555 PyObject *self;
1556 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001557{
1558 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001559 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001560 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001561#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001562 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1563 APIRET rc;
1564 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001565 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001566
1567 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1568 APIRET rc;
1569 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001570 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001571
1572 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001573 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001574#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001575 if (kill(pid, sig) == -1)
1576 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001577#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001578 Py_INCREF(Py_None);
1579 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001580}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001581#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001582
Guido van Rossumc0125471996-06-28 18:55:32 +00001583#ifdef HAVE_PLOCK
1584
1585#ifdef HAVE_SYS_LOCK_H
1586#include <sys/lock.h>
1587#endif
1588
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001589static char posix_plock__doc__[] =
1590"plock(op) -> None\n\
1591Lock program segments into memory.";
1592
Barry Warsaw53699e91996-12-10 23:23:01 +00001593static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001594posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001595 PyObject *self;
1596 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001597{
1598 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001599 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001600 return NULL;
1601 if (plock(op) == -1)
1602 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001603 Py_INCREF(Py_None);
1604 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001605}
1606#endif
1607
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001608
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001609#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001610static char posix_popen__doc__[] =
1611"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1612Open a pipe to/from a command returning a file object.";
1613
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001614#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001615static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001616async_system(const char *command)
1617{
1618 char *p, errormsg[256], args[1024];
1619 RESULTCODES rcodes;
1620 APIRET rc;
1621 char *shell = getenv("COMSPEC");
1622 if (!shell)
1623 shell = "cmd";
1624
1625 strcpy(args, shell);
1626 p = &args[ strlen(args)+1 ];
1627 strcpy(p, "/c ");
1628 strcat(p, command);
1629 p += strlen(p) + 1;
1630 *p = '\0';
1631
1632 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001633 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001634 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001635 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001636 &rcodes, shell);
1637 return rc;
1638}
1639
Guido van Rossumd48f2521997-12-05 22:19:34 +00001640static FILE *
1641popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001642{
1643 HFILE rhan, whan;
1644 FILE *retfd = NULL;
1645 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1646
Guido van Rossumd48f2521997-12-05 22:19:34 +00001647 if (rc != NO_ERROR) {
1648 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001649 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001650 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001651
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001652 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1653 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001654
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001655 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1656 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001657
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001658 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1659 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001660
1661 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001662 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001663 }
1664
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001665 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1666 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001667
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001668 close(oldfd); /* And Close Saved STDOUT Handle */
1669 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001670
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001671 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1672 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001673
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001674 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1675 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001676
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001677 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1678 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001679
1680 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001681 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001682 }
1683
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001684 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1685 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001686
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001687 close(oldfd); /* And Close Saved STDIN Handle */
1688 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001689
Guido van Rossumd48f2521997-12-05 22:19:34 +00001690 } else {
1691 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001692 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001693 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001694}
1695
1696static PyObject *
1697posix_popen(self, args)
1698 PyObject *self;
1699 PyObject *args;
1700{
1701 char *name;
1702 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001703 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001704 FILE *fp;
1705 PyObject *f;
1706 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1707 return NULL;
1708 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001709 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001710 Py_END_ALLOW_THREADS
1711 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001712 return os2_error(err);
1713
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001714 f = PyFile_FromFile(fp, name, mode, fclose);
1715 if (f != NULL)
1716 PyFile_SetBufSize(f, bufsize);
1717 return f;
1718}
1719
1720#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001721static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001722posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001723 PyObject *self;
1724 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001725{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001726 char *name;
1727 char *mode = "r";
1728 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001729 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001730 PyObject *f;
1731 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001732 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001733 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001734 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001735 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001736 if (fp == NULL)
1737 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001738 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001739 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001740 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001741 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001742}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001743#endif
1744
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001745#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001746
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001747
Guido van Rossumb6775db1994-08-01 11:34:53 +00001748#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001749static char posix_setuid__doc__[] =
1750"setuid(uid) -> None\n\
1751Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001752static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001753posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001754 PyObject *self;
1755 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001756{
1757 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001758 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001759 return NULL;
1760 if (setuid(uid) < 0)
1761 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001762 Py_INCREF(Py_None);
1763 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001764}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001765#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001766
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001767
Guido van Rossumb6775db1994-08-01 11:34:53 +00001768#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001769static char posix_setgid__doc__[] =
1770"setgid(gid) -> None\n\
1771Set the current process's group id.";
1772
Barry Warsaw53699e91996-12-10 23:23:01 +00001773static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001774posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001775 PyObject *self;
1776 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001777{
1778 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001779 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001780 return NULL;
1781 if (setgid(gid) < 0)
1782 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001783 Py_INCREF(Py_None);
1784 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001785}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001786#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001787
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001788
Guido van Rossumb6775db1994-08-01 11:34:53 +00001789#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001790static char posix_waitpid__doc__[] =
1791"waitpid(pid, options) -> (pid, status)\n\
1792Wait for completion of a give child process.";
1793
Barry Warsaw53699e91996-12-10 23:23:01 +00001794static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001795posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001796 PyObject *self;
1797 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001798{
Guido van Rossumfd03e2b1996-06-19 23:17:02 +00001799 int pid, options, sts = 0;
Barry Warsaw53699e91996-12-10 23:23:01 +00001800 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00001801 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001802 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001803#ifdef NeXT
1804 pid = wait4(pid, (union wait *)&sts, options, NULL);
1805#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001806 pid = waitpid(pid, &sts, options);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001807#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001808 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00001809 if (pid == -1)
1810 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00001811 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001812 return Py_BuildValue("ii", pid, sts);
Guido van Rossum21803b81992-08-09 12:55:27 +00001813}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001814#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00001815
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001816
Guido van Rossumad0ee831995-03-01 10:34:45 +00001817#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001818static char posix_wait__doc__[] =
1819"wait() -> (pid, status)\n\
1820Wait for completion of a child process.";
1821
Barry Warsaw53699e91996-12-10 23:23:01 +00001822static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00001823posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001824 PyObject *self;
1825 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00001826{
1827 int pid, sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001828 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001829#ifdef NeXT
1830 pid = wait((union wait *)&sts);
1831#else
Guido van Rossum21803b81992-08-09 12:55:27 +00001832 pid = wait(&sts);
Guido van Rossumb9f866c1997-05-22 15:12:39 +00001833#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001834 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00001835 if (pid == -1)
1836 return posix_error();
1837 else
Barry Warsaw53699e91996-12-10 23:23:01 +00001838 return Py_BuildValue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001839}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001840#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001841
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001842
1843static char posix_lstat__doc__[] =
1844"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1845Like stat(path), but do not follow symbolic links.";
1846
Barry Warsaw53699e91996-12-10 23:23:01 +00001847static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001848posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001849 PyObject *self;
1850 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001851{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001852#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001853 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00001854#else /* !HAVE_LSTAT */
1855 return posix_do_stat(self, args, stat);
1856#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001857}
1858
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001859
Guido van Rossumb6775db1994-08-01 11:34:53 +00001860#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001861static char posix_readlink__doc__[] =
1862"readlink(path) -> path\n\
1863Return a string representing the path to which the symbolic link points.";
1864
Barry Warsaw53699e91996-12-10 23:23:01 +00001865static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001866posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001867 PyObject *self;
1868 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001869{
Guido van Rossumb6775db1994-08-01 11:34:53 +00001870 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001871 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001872 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00001873 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001874 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001875 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001876 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00001877 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001878 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001879 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001880 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001881}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001882#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001883
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001884
Guido van Rossumb6775db1994-08-01 11:34:53 +00001885#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001886static char posix_symlink__doc__[] =
1887"symlink(src, dst) -> None\n\
1888Create a symbolic link.";
1889
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001890static PyObject *
1891posix_symlink(self, args)
1892 PyObject *self;
1893 PyObject *args;
1894{
1895 return posix_2str(args, symlink);
1896}
1897#endif /* HAVE_SYMLINK */
1898
1899
1900#ifdef HAVE_TIMES
1901#ifndef HZ
1902#define HZ 60 /* Universal constant :-) */
1903#endif /* HZ */
1904
Guido van Rossumd48f2521997-12-05 22:19:34 +00001905#if defined(PYCC_VACPP) && defined(PYOS_OS2)
1906static long
1907system_uptime()
1908{
1909 ULONG value = 0;
1910
1911 Py_BEGIN_ALLOW_THREADS
1912 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
1913 Py_END_ALLOW_THREADS
1914
1915 return value;
1916}
1917
1918static PyObject *
1919posix_times(self, args)
1920 PyObject *self;
1921 PyObject *args;
1922{
1923 if (!PyArg_NoArgs(args))
1924 return NULL;
1925
1926 /* Currently Only Uptime is Provided -- Others Later */
1927 return Py_BuildValue("ddddd",
1928 (double)0 /* t.tms_utime / HZ */,
1929 (double)0 /* t.tms_stime / HZ */,
1930 (double)0 /* t.tms_cutime / HZ */,
1931 (double)0 /* t.tms_cstime / HZ */,
1932 (double)system_uptime() / 1000);
1933}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001934#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00001935static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00001936posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001937 PyObject *self;
1938 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00001939{
1940 struct tms t;
1941 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00001942 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00001943 return NULL;
1944 errno = 0;
1945 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00001946 if (c == (clock_t) -1)
1947 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001948 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001949 (double)t.tms_utime / HZ,
1950 (double)t.tms_stime / HZ,
1951 (double)t.tms_cutime / HZ,
1952 (double)t.tms_cstime / HZ,
1953 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00001954}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001955#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001956#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001957
1958
Guido van Rossum87755a21996-09-07 00:59:43 +00001959#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001960#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00001961static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001962posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001963 PyObject *self;
1964 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001965{
1966 FILETIME create, exit, kernel, user;
1967 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001968 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001969 return NULL;
1970 hProc = GetCurrentProcess();
1971 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00001972 return Py_BuildValue(
1973 "ddddd",
1974 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
1975 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
1976 (double)0,
1977 (double)0,
1978 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00001979}
Guido van Rossum8d665e61996-06-26 18:22:49 +00001980#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001981
1982#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00001983static char posix_times__doc__[] =
1984"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
1985Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00001986#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00001987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001988
Guido van Rossumb6775db1994-08-01 11:34:53 +00001989#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001990static char posix_setsid__doc__[] =
1991"setsid() -> None\n\
1992Call the system call setsid().";
1993
Barry Warsaw53699e91996-12-10 23:23:01 +00001994static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001995posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001996 PyObject *self;
1997 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001998{
Barry Warsaw53699e91996-12-10 23:23:01 +00001999 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002000 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002001 if (setsid() < 0)
2002 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002003 Py_INCREF(Py_None);
2004 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002005}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002006#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002007
Guido van Rossumb6775db1994-08-01 11:34:53 +00002008#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002009static char posix_setpgid__doc__[] =
2010"setpgid(pid, pgrp) -> None\n\
2011Call the system call setpgid().";
2012
Barry Warsaw53699e91996-12-10 23:23:01 +00002013static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002014posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002015 PyObject *self;
2016 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002017{
2018 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002019 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002020 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002021 if (setpgid(pid, pgrp) < 0)
2022 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002023 Py_INCREF(Py_None);
2024 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002025}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002026#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002027
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002028
Guido van Rossumb6775db1994-08-01 11:34:53 +00002029#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002030static char posix_tcgetpgrp__doc__[] =
2031"tcgetpgrp(fd) -> pgid\n\
2032Return the process group associated with the terminal given by a fd.";
2033
Barry Warsaw53699e91996-12-10 23:23:01 +00002034static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002035posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002036 PyObject *self;
2037 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002038{
2039 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002040 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002041 return NULL;
2042 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002043 if (pgid < 0)
2044 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002045 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002046}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002047#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002049
Guido van Rossumb6775db1994-08-01 11:34:53 +00002050#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002051static char posix_tcsetpgrp__doc__[] =
2052"tcsetpgrp(fd, pgid) -> None\n\
2053Set the process group associated with the terminal given by a fd.";
2054
Barry Warsaw53699e91996-12-10 23:23:01 +00002055static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002056posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002057 PyObject *self;
2058 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002059{
2060 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002061 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002062 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002063 if (tcsetpgrp(fd, pgid) < 0)
2064 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002065 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002066 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002067}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002068#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002069
Guido van Rossum687dd131993-05-17 08:34:16 +00002070/* Functions acting on file descriptors */
2071
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002072static char posix_open__doc__[] =
2073"open(filename, flag [, mode=0777]) -> fd\n\
2074Open a file (for low level IO).";
2075
Barry Warsaw53699e91996-12-10 23:23:01 +00002076static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002077posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002078 PyObject *self;
2079 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002080{
2081 char *file;
2082 int flag;
2083 int mode = 0777;
2084 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002085 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2086 return NULL;
2087
Barry Warsaw53699e91996-12-10 23:23:01 +00002088 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002089 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002090 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002091 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002092 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002093 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002094}
2095
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002096
2097static char posix_close__doc__[] =
2098"close(fd) -> None\n\
2099Close a file descriptor (for low level IO).";
2100
Barry Warsaw53699e91996-12-10 23:23:01 +00002101static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002102posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002103 PyObject *self;
2104 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002105{
2106 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002107 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002108 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002109 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002110 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002111 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002112 if (res < 0)
2113 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002114 Py_INCREF(Py_None);
2115 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002116}
2117
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002118
2119static char posix_dup__doc__[] =
2120"dup(fd) -> fd2\n\
2121Return a duplicate of a file descriptor.";
2122
Barry Warsaw53699e91996-12-10 23:23:01 +00002123static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002124posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002125 PyObject *self;
2126 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002127{
2128 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002129 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002130 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002131 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002132 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002133 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002134 if (fd < 0)
2135 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002136 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002137}
2138
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002139
2140static char posix_dup2__doc__[] =
2141"dup2(fd, fd2) -> None\n\
2142Duplicate file descriptor.";
2143
Barry Warsaw53699e91996-12-10 23:23:01 +00002144static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002145posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002146 PyObject *self;
2147 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002148{
2149 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002150 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002151 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002152 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002153 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002154 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002155 if (res < 0)
2156 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002157 Py_INCREF(Py_None);
2158 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002159}
2160
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002161
2162static char posix_lseek__doc__[] =
2163"lseek(fd, pos, how) -> newpos\n\
2164Set the current position of a file descriptor.";
2165
Barry Warsaw53699e91996-12-10 23:23:01 +00002166static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002167posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002168 PyObject *self;
2169 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002170{
2171 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002172 off_t pos, res;
2173 PyObject *posobj;
2174 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002175 return NULL;
2176#ifdef SEEK_SET
2177 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2178 switch (how) {
2179 case 0: how = SEEK_SET; break;
2180 case 1: how = SEEK_CUR; break;
2181 case 2: how = SEEK_END; break;
2182 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002183#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002184
2185#if !defined(HAVE_LARGEFILE_SUPPORT)
2186 pos = PyInt_AsLong(posobj);
2187#else
2188 pos = PyLong_Check(posobj) ?
2189 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2190#endif
2191 if (PyErr_Occurred())
2192 return NULL;
2193
Barry Warsaw53699e91996-12-10 23:23:01 +00002194 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002195 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002196 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002197 if (res < 0)
2198 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002199
2200#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002201 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002202#else
2203 return PyLong_FromLongLong(res);
2204#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002205}
2206
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002207
2208static char posix_read__doc__[] =
2209"read(fd, buffersize) -> string\n\
2210Read a file descriptor.";
2211
Barry Warsaw53699e91996-12-10 23:23:01 +00002212static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002213posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002214 PyObject *self;
2215 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002216{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002217 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002218 PyObject *buffer;
2219 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002220 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002221 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002222 if (buffer == NULL)
2223 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002224 Py_BEGIN_ALLOW_THREADS
2225 n = read(fd, PyString_AsString(buffer), size);
2226 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002227 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002228 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002229 return posix_error();
2230 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002231 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002232 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002233 return buffer;
2234}
2235
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002236
2237static char posix_write__doc__[] =
2238"write(fd, string) -> byteswritten\n\
2239Write a string to a file descriptor.";
2240
Barry Warsaw53699e91996-12-10 23:23:01 +00002241static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002242posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002243 PyObject *self;
2244 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002245{
2246 int fd, size;
2247 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002248 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002249 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002250 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002251 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002252 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002253 if (size < 0)
2254 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002255 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002256}
2257
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002258
2259static char posix_fstat__doc__[]=
2260"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2261Like stat(), but for an open file descriptor.";
2262
Barry Warsaw53699e91996-12-10 23:23:01 +00002263static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002264posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002265 PyObject *self;
2266 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002267{
2268 int fd;
2269 struct stat st;
2270 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002271 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002272 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002273 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002274 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002275 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002276 if (res != 0)
2277 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002278#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002279 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002280 (long)st.st_mode,
2281 (long)st.st_ino,
2282 (long)st.st_dev,
2283 (long)st.st_nlink,
2284 (long)st.st_uid,
2285 (long)st.st_gid,
2286 (long)st.st_size,
2287 (long)st.st_atime,
2288 (long)st.st_mtime,
2289 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002290#else
2291 return Py_BuildValue("(lLllllLlll)",
2292 (long)st.st_mode,
2293 (LONG_LONG)st.st_ino,
2294 (long)st.st_dev,
2295 (long)st.st_nlink,
2296 (long)st.st_uid,
2297 (long)st.st_gid,
2298 (LONG_LONG)st.st_size,
2299 (long)st.st_atime,
2300 (long)st.st_mtime,
2301 (long)st.st_ctime);
2302#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002303}
2304
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002305
2306static char posix_fdopen__doc__[] =
2307"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2308Return an open file object connected to a file descriptor.";
2309
Barry Warsaw53699e91996-12-10 23:23:01 +00002310static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002311posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002312 PyObject *self;
2313 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002314{
Barry Warsaw53699e91996-12-10 23:23:01 +00002315 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002316 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002317 char *mode = "r";
2318 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002319 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002320 PyObject *f;
2321 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002322 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002323
Barry Warsaw53699e91996-12-10 23:23:01 +00002324 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002325 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002326 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002327 if (fp == NULL)
2328 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002329 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002330 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002331 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002332 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002333}
2334
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002335
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002336#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002337static char posix_pipe__doc__[] =
2338"pipe() -> (read_end, write_end)\n\
2339Create a pipe.";
2340
Barry Warsaw53699e91996-12-10 23:23:01 +00002341static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002342posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002343 PyObject *self;
2344 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002345{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002346#if defined(PYOS_OS2)
2347 HFILE read, write;
2348 APIRET rc;
2349
2350 if (!PyArg_Parse(args, ""))
2351 return NULL;
2352
2353 Py_BEGIN_ALLOW_THREADS
2354 rc = DosCreatePipe( &read, &write, 4096);
2355 Py_END_ALLOW_THREADS
2356 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002357 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002358
2359 return Py_BuildValue("(ii)", read, write);
2360#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002361#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002362 int fds[2];
2363 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002364 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002365 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002366 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002367 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002368 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002369 if (res != 0)
2370 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002371 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002372#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002373 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002374 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002375 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002376 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002377 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002378 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002379 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002380 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002381 if (!ok)
2382 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002383 read_fd = _open_osfhandle((long)read, 0);
2384 write_fd = _open_osfhandle((long)write, 1);
2385 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002386#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002387#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002388}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002389#endif /* HAVE_PIPE */
2390
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002391
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002392#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002393static char posix_mkfifo__doc__[] =
2394"mkfifo(file, [, mode=0666]) -> None\n\
2395Create a FIFO (a POSIX named pipe).";
2396
Barry Warsaw53699e91996-12-10 23:23:01 +00002397static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002398posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002399 PyObject *self;
2400 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002401{
2402 char *file;
2403 int mode = 0666;
2404 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002405 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002406 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002407 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002408 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002409 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002410 if (res < 0)
2411 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002412 Py_INCREF(Py_None);
2413 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002414}
2415#endif
2416
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002417
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002418#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002419static char posix_ftruncate__doc__[] =
2420"ftruncate(fd, length) -> None\n\
2421Truncate a file to a specified length.";
2422
Barry Warsaw53699e91996-12-10 23:23:01 +00002423static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002424posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002425 PyObject *self; /* Not used */
2426 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002427{
2428 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002429 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002430 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002431 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002432
Guido van Rossum94f6f721999-01-06 18:42:14 +00002433 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2434 return NULL;
2435
2436#if !defined(HAVE_LARGEFILE_SUPPORT)
2437 length = PyInt_AsLong(lenobj);
2438#else
2439 length = PyLong_Check(lenobj) ?
2440 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2441#endif
2442 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002443 return NULL;
2444
Barry Warsaw53699e91996-12-10 23:23:01 +00002445 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002446 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002447 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002448 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002449 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002450 return NULL;
2451 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002452 Py_INCREF(Py_None);
2453 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002454}
2455#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002456
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002457#ifdef NeXT
2458#define HAVE_PUTENV
2459/* Steve Spicklemire got this putenv from NeXTAnswers */
2460static int
2461putenv(char *newval)
2462{
2463 extern char **environ;
2464
2465 static int firstTime = 1;
2466 char **ep;
2467 char *cp;
2468 int esiz;
2469 char *np;
2470
2471 if (!(np = strchr(newval, '=')))
2472 return 1;
2473 *np = '\0';
2474
2475 /* look it up */
2476 for (ep=environ ; *ep ; ep++)
2477 {
2478 /* this should always be true... */
2479 if (cp = strchr(*ep, '='))
2480 {
2481 *cp = '\0';
2482 if (!strcmp(*ep, newval))
2483 {
2484 /* got it! */
2485 *cp = '=';
2486 break;
2487 }
2488 *cp = '=';
2489 }
2490 else
2491 {
2492 *np = '=';
2493 return 1;
2494 }
2495 }
2496
2497 *np = '=';
2498 if (*ep)
2499 {
2500 /* the string was already there:
2501 just replace it with the new one */
2502 *ep = newval;
2503 return 0;
2504 }
2505
2506 /* expand environ by one */
2507 for (esiz=2, ep=environ ; *ep ; ep++)
2508 esiz++;
2509 if (firstTime)
2510 {
2511 char **epp;
2512 char **newenv;
2513 if (!(newenv = malloc(esiz * sizeof(char *))))
2514 return 1;
2515
2516 for (ep=environ, epp=newenv ; *ep ;)
2517 *epp++ = *ep++;
2518 *epp++ = newval;
2519 *epp = (char *) 0;
2520 environ = newenv;
2521 }
2522 else
2523 {
2524 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2525 return 1;
2526 environ[esiz - 2] = newval;
2527 environ[esiz - 1] = (char *) 0;
2528 firstTime = 0;
2529 }
2530
2531 return 0;
2532}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002533#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002534
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002535
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002536#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002537static char posix_putenv__doc__[] =
2538"putenv(key, value) -> None\n\
2539Change or add an environment variable.";
2540
Guido van Rossumbcc20741998-08-04 22:53:56 +00002541#ifdef __BEOS__
2542/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2543int putenv( const char *str );
2544#endif
2545
Barry Warsaw53699e91996-12-10 23:23:01 +00002546static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002547posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002548 PyObject *self;
2549 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002550{
2551 char *s1, *s2;
2552 char *new;
2553
Barry Warsaw53699e91996-12-10 23:23:01 +00002554 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002555 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002556
2557#if defined(PYOS_OS2)
2558 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2559 APIRET rc;
2560
2561 if (strlen(s2) == 0) /* If New Value is an Empty String */
2562 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2563
2564 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2565 if (rc != NO_ERROR)
2566 return os2_error(rc);
2567
2568 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2569 APIRET rc;
2570
2571 if (strlen(s2) == 0) /* If New Value is an Empty String */
2572 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2573
2574 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2575 if (rc != NO_ERROR)
2576 return os2_error(rc);
2577 } else {
2578#endif
2579
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002580 /* XXX This leaks memory -- not easy to fix :-( */
2581 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002582 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002583 (void) sprintf(new, "%s=%s", s1, s2);
2584 if (putenv(new)) {
2585 posix_error();
2586 return NULL;
2587 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002588
2589#if defined(PYOS_OS2)
2590 }
2591#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002592 Py_INCREF(Py_None);
2593 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002594}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002595#endif /* putenv */
2596
2597#ifdef HAVE_STRERROR
2598static char posix_strerror__doc__[] =
2599"strerror(code) -> string\n\
2600Translate an error code to a message string.";
2601
2602PyObject *
2603posix_strerror(self, args)
2604 PyObject *self;
2605 PyObject *args;
2606{
2607 int code;
2608 char *message;
2609 if (!PyArg_ParseTuple(args, "i", &code))
2610 return NULL;
2611 message = strerror(code);
2612 if (message == NULL) {
2613 PyErr_SetString(PyExc_ValueError,
2614 "strerror code out of range");
2615 return NULL;
2616 }
2617 return PyString_FromString(message);
2618}
2619#endif /* strerror */
2620
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002621
Guido van Rossumc9641791998-08-04 15:26:23 +00002622#ifdef HAVE_SYS_WAIT_H
2623
2624#ifdef WIFSTOPPED
2625static char posix_WIFSTOPPED__doc__[] =
2626"WIFSTOPPED(status) -> Boolean\n\
2627See Unix documentation.";
2628
2629static PyObject *
2630posix_WIFSTOPPED(self, args)
2631 PyObject *self;
2632 PyObject *args;
2633{
2634 int status = 0;
2635
2636 if (!PyArg_Parse(args, "i", &status))
2637 {
2638 return NULL;
2639 }
2640
2641 return Py_BuildValue("i", WIFSTOPPED(status));
2642}
2643#endif /* WIFSTOPPED */
2644
2645#ifdef WIFSIGNALED
2646static char posix_WIFSIGNALED__doc__[] =
2647"WIFSIGNALED(status) -> Boolean\n\
2648See Unix documentation.";
2649
2650static PyObject *
2651posix_WIFSIGNALED(self, args)
2652 PyObject *self;
2653 PyObject *args;
2654{
2655 int status = 0;
2656
2657 if (!PyArg_Parse(args, "i", &status))
2658 {
2659 return NULL;
2660 }
2661
2662 return Py_BuildValue("i", WIFSIGNALED(status));
2663}
2664#endif /* WIFSIGNALED */
2665
2666#ifdef WIFEXITED
2667static char posix_WIFEXITED__doc__[] =
2668"WIFEXITED(status) -> Boolean\n\
2669See Unix documentation.";
2670
2671static PyObject *
2672posix_WIFEXITED(self, args)
2673 PyObject *self;
2674 PyObject *args;
2675{
2676 int status = 0;
2677
2678 if (!PyArg_Parse(args, "i", &status))
2679 {
2680 return NULL;
2681 }
2682
2683 return Py_BuildValue("i", WIFEXITED(status));
2684}
2685#endif /* WIFEXITED */
2686
2687#ifdef WIFSTOPPED
2688static char posix_WEXITSTATUS__doc__[] =
2689"WEXITSTATUS(status) -> integer\n\
2690See Unix documentation.";
2691
2692static PyObject *
2693posix_WEXITSTATUS(self, args)
2694 PyObject *self;
2695 PyObject *args;
2696{
2697 int status = 0;
2698
2699 if (!PyArg_Parse(args, "i", &status))
2700 {
2701 return NULL;
2702 }
2703
2704 return Py_BuildValue("i", WEXITSTATUS(status));
2705}
2706#endif /* WEXITSTATUS */
2707
2708#ifdef WTERMSIG
2709static char posix_WTERMSIG__doc__[] =
2710"WTERMSIG(status) -> integer\n\
2711See Unix documentation.";
2712
2713static PyObject *
2714posix_WTERMSIG(self, args)
2715 PyObject *self;
2716 PyObject *args;
2717{
2718 int status = 0;
2719
2720 if (!PyArg_Parse(args, "i", &status))
2721 {
2722 return NULL;
2723 }
2724
2725 return Py_BuildValue("i", WTERMSIG(status));
2726}
2727#endif /* WTERMSIG */
2728
2729#ifdef WSTOPSIG
2730static char posix_WSTOPSIG__doc__[] =
2731"WSTOPSIG(status) -> integer\n\
2732See Unix documentation.";
2733
2734static PyObject *
2735posix_WSTOPSIG(self, args)
2736 PyObject *self;
2737 PyObject *args;
2738{
2739 int status = 0;
2740
2741 if (!PyArg_Parse(args, "i", &status))
2742 {
2743 return NULL;
2744 }
2745
2746 return Py_BuildValue("i", WSTOPSIG(status));
2747}
2748#endif /* WSTOPSIG */
2749
2750#endif /* HAVE_SYS_WAIT_H */
2751
2752
Guido van Rossum94f6f721999-01-06 18:42:14 +00002753#if defined(HAVE_FSTATVFS)
2754#include <sys/statvfs.h>
2755
2756static char posix_fstatvfs__doc__[] =
2757"fstatvfs(fd) -> \
2758(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
2759Perform an fstatvfs system call on the given fd.";
2760
2761static PyObject *
2762posix_fstatvfs(self, args)
2763 PyObject *self;
2764 PyObject *args;
2765{
2766 int fd, res;
2767 struct statvfs st;
2768 if (!PyArg_ParseTuple(args, "i", &fd))
2769 return NULL;
2770 Py_BEGIN_ALLOW_THREADS
2771 res = fstatvfs(fd, &st);
2772 Py_END_ALLOW_THREADS
2773 if (res != 0)
2774 return posix_error();
2775#if !defined(HAVE_LARGEFILE_SUPPORT)
2776 return Py_BuildValue("(lllllllllll)",
2777 (long) st.f_bsize,
2778 (long) st.f_frsize,
2779 (long) st.f_blocks,
2780 (long) st.f_bfree,
2781 (long) st.f_bavail,
2782 (long) st.f_files,
2783 (long) st.f_ffree,
2784 (long) st.f_favail,
2785 (long) st.f_fsid,
2786 (long) st.f_flag,
2787 (long) st.f_namemax);
2788#else
2789 return Py_BuildValue("(llLLLLLLlll)",
2790 (long) st.f_bsize,
2791 (long) st.f_frsize,
2792 (LONG_LONG) st.f_blocks,
2793 (LONG_LONG) st.f_bfree,
2794 (LONG_LONG) st.f_bavail,
2795 (LONG_LONG) st.f_files,
2796 (LONG_LONG) st.f_ffree,
2797 (LONG_LONG) st.f_favail,
2798 (long) st.f_fsid,
2799 (long) st.f_flag,
2800 (long) st.f_namemax);
2801#endif
2802}
2803#endif /* HAVE_FSTATVFS */
2804
2805
2806#if defined(HAVE_STATVFS)
2807#include <sys/statvfs.h>
2808
2809static char posix_statvfs__doc__[] =
2810"statvfs(path) -> \
2811(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
2812Perform a statvfs system call on the given path.";
2813
2814static PyObject *
2815posix_statvfs(self, args)
2816 PyObject *self;
2817 PyObject *args;
2818{
2819 char *path;
2820 int res;
2821 struct statvfs st;
2822 if (!PyArg_ParseTuple(args, "s", &path))
2823 return NULL;
2824 Py_BEGIN_ALLOW_THREADS
2825 res = statvfs(path, &st);
2826 Py_END_ALLOW_THREADS
2827 if (res != 0)
2828 return posix_error_with_filename(path);
2829#if !defined(HAVE_LARGEFILE_SUPPORT)
2830 return Py_BuildValue("(lllllllllll)",
2831 (long) st.f_bsize,
2832 (long) st.f_frsize,
2833 (long) st.f_blocks,
2834 (long) st.f_bfree,
2835 (long) st.f_bavail,
2836 (long) st.f_files,
2837 (long) st.f_ffree,
2838 (long) st.f_favail,
2839 (long) st.f_fsid,
2840 (long) st.f_flag,
2841 (long) st.f_namemax);
2842#else /* HAVE_LARGEFILE_SUPPORT */
2843 return Py_BuildValue("(llLLLLLLlll)",
2844 (long) st.f_bsize,
2845 (long) st.f_frsize,
2846 (LONG_LONG) st.f_blocks,
2847 (LONG_LONG) st.f_bfree,
2848 (LONG_LONG) st.f_bavail,
2849 (LONG_LONG) st.f_files,
2850 (LONG_LONG) st.f_ffree,
2851 (LONG_LONG) st.f_favail,
2852 (long) st.f_fsid,
2853 (long) st.f_flag,
2854 (long) st.f_namemax);
2855#endif
2856}
2857#endif /* HAVE_STATVFS */
2858
2859
Barry Warsaw53699e91996-12-10 23:23:01 +00002860static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00002861 {"access", posix_access, 0, posix_access__doc__},
2862 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002863 {"chdir", posix_chdir, 0, posix_chdir__doc__},
2864 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002865#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002866 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002867#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00002868#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002869 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00002870#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00002871#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002872 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002873#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002874 {"listdir", posix_listdir, 0, posix_listdir__doc__},
2875 {"lstat", posix_lstat, 0, posix_lstat__doc__},
2876 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002877#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002878 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002879#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002880#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002881 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002882#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002883 {"rename", posix_rename, 0, posix_rename__doc__},
2884 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
2885 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002886#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002887 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002888#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002889#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002890 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002891#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002892 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002893#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002894 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002895#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002896 {"unlink", posix_unlink, 0, posix_unlink__doc__},
2897 {"remove", posix_unlink, 0, posix_remove__doc__},
2898 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002899#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002900 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002901#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002902 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002903#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002904 {"execv", posix_execv, 0, posix_execv__doc__},
2905 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002906#endif /* HAVE_EXECV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002907#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002908 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002909#endif /* HAVE_FORK */
2910#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002911 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002912#endif /* HAVE_GETEGID */
2913#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002914 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002915#endif /* HAVE_GETEUID */
2916#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002917 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002918#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002919 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00002920#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002921 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002922#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002923#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002924 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002925#endif /* HAVE_GETPPID */
2926#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002927 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002928#endif /* HAVE_GETUID */
2929#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002930 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002931#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00002932#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002933 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00002934#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002935#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002936 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002937#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002938#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002939 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002940#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002941#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002942 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002943#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002944#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002945 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002946#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00002947#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002948 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00002949#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002950#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002951 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002952#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002953#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002954 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002955#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002956#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002957 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002958#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002959#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002960 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002961#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002962#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002963 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002964#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002965 {"open", posix_open, 1, posix_open__doc__},
2966 {"close", posix_close, 0, posix_close__doc__},
2967 {"dup", posix_dup, 0, posix_dup__doc__},
2968 {"dup2", posix_dup2, 0, posix_dup2__doc__},
2969 {"lseek", posix_lseek, 0, posix_lseek__doc__},
2970 {"read", posix_read, 0, posix_read__doc__},
2971 {"write", posix_write, 0, posix_write__doc__},
2972 {"fstat", posix_fstat, 0, posix_fstat__doc__},
2973 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002974#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002975 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002976#endif
2977#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002978 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002979#endif
2980#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002981 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002982#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002983#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002984 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002985#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00002986#ifdef HAVE_STRERROR
2987 {"strerror", posix_strerror, 1, posix_strerror__doc__},
2988#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00002989#ifdef HAVE_FSYNC
2990 {"fsync", posix_fsync, 0, posix_fsync__doc__},
2991#endif
2992#ifdef HAVE_FDATASYNC
2993 {"fdatasync", posix_fdatasync, 0, posix_fdatasync__doc__},
2994#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00002995#ifdef HAVE_SYS_WAIT_H
2996#ifdef WIFSTOPPED
2997 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
2998#endif /* WIFSTOPPED */
2999#ifdef WIFSIGNALED
3000 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
3001#endif /* WIFSIGNALED */
3002#ifdef WIFEXITED
3003 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
3004#endif /* WIFEXITED */
3005#ifdef WEXITSTATUS
3006 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
3007#endif /* WEXITSTATUS */
3008#ifdef WTERMSIG
3009 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
3010#endif /* WTERMSIG */
3011#ifdef WSTOPSIG
3012 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
3013#endif /* WSTOPSIG */
3014#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003015#ifdef HAVE_FSTATVFS
3016 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
3017#endif
3018#ifdef HAVE_STATVFS
3019 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
3020#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003021 {NULL, NULL} /* Sentinel */
3022};
3023
3024
Barry Warsaw4a342091996-12-19 23:50:02 +00003025static int
3026ins(d, symbol, value)
3027 PyObject* d;
3028 char* symbol;
3029 long value;
3030{
3031 PyObject* v = PyInt_FromLong(value);
3032 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
3033 return -1; /* triggers fatal error */
3034
3035 Py_DECREF(v);
3036 return 0;
3037}
3038
Guido van Rossumd48f2521997-12-05 22:19:34 +00003039#if defined(PYOS_OS2)
3040/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
3041static int insertvalues(PyObject *d)
3042{
3043 APIRET rc;
3044 ULONG values[QSV_MAX+1];
3045 PyObject *v;
3046 char *ver, tmp[10];
3047
3048 Py_BEGIN_ALLOW_THREADS
3049 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
3050 Py_END_ALLOW_THREADS
3051
3052 if (rc != NO_ERROR) {
3053 os2_error(rc);
3054 return -1;
3055 }
3056
3057 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3058 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3059 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3060 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3061 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3062 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3063 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3064
3065 switch (values[QSV_VERSION_MINOR]) {
3066 case 0: ver = "2.00"; break;
3067 case 10: ver = "2.10"; break;
3068 case 11: ver = "2.11"; break;
3069 case 30: ver = "3.00"; break;
3070 case 40: ver = "4.00"; break;
3071 case 50: ver = "5.00"; break;
3072 default:
3073 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3074 values[QSV_VERSION_MINOR]);
3075 ver = &tmp[0];
3076 }
3077
3078 /* Add Indicator of the Version of the Operating System */
3079 v = PyString_FromString(ver);
3080 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3081 return -1;
3082 Py_DECREF(v);
3083
3084 /* Add Indicator of Which Drive was Used to Boot the System */
3085 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3086 tmp[1] = ':';
3087 tmp[2] = '\0';
3088
3089 v = PyString_FromString(tmp);
3090 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3091 return -1;
3092 Py_DECREF(v);
3093
3094 return 0;
3095}
3096#endif
3097
Barry Warsaw4a342091996-12-19 23:50:02 +00003098static int
3099all_ins(d)
3100 PyObject* d;
3101{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003102#ifdef F_OK
3103 if (ins(d, "F_OK", (long)F_OK)) return -1;
3104#endif
3105#ifdef R_OK
3106 if (ins(d, "R_OK", (long)R_OK)) return -1;
3107#endif
3108#ifdef W_OK
3109 if (ins(d, "W_OK", (long)W_OK)) return -1;
3110#endif
3111#ifdef X_OK
3112 if (ins(d, "X_OK", (long)X_OK)) return -1;
3113#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003114#ifdef WNOHANG
3115 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3116#endif
3117#ifdef O_RDONLY
3118 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3119#endif
3120#ifdef O_WRONLY
3121 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3122#endif
3123#ifdef O_RDWR
3124 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3125#endif
3126#ifdef O_NDELAY
3127 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3128#endif
3129#ifdef O_NONBLOCK
3130 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3131#endif
3132#ifdef O_APPEND
3133 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3134#endif
3135#ifdef O_DSYNC
3136 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3137#endif
3138#ifdef O_RSYNC
3139 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3140#endif
3141#ifdef O_SYNC
3142 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3143#endif
3144#ifdef O_NOCTTY
3145 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3146#endif
3147#ifdef O_CREAT
3148 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3149#endif
3150#ifdef O_EXCL
3151 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3152#endif
3153#ifdef O_TRUNC
3154 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3155#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003156#ifdef O_BINARY
3157 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3158#endif
3159#ifdef O_TEXT
3160 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3161#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003162
3163#if defined(PYOS_OS2)
3164 if (insertvalues(d)) return -1;
3165#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003166 return 0;
3167}
3168
3169
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003170#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003171#define INITFUNC initnt
3172#define MODNAME "nt"
3173#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003174#if defined(PYOS_OS2)
3175#define INITFUNC initos2
3176#define MODNAME "os2"
3177#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003178#define INITFUNC initposix
3179#define MODNAME "posix"
3180#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003181#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003182
Guido van Rossum3886bb61998-12-04 18:50:17 +00003183DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003184INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003185{
Barry Warsaw53699e91996-12-10 23:23:01 +00003186 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003187
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003188 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003189 posix_methods,
3190 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003191 (PyObject *)NULL,
3192 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003193 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003194
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003195 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003196 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003197 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003198 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003199 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003200
Barry Warsaw4a342091996-12-19 23:50:02 +00003201 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003202 return;
3203
Barry Warsawd58d7641998-07-23 16:14:40 +00003204 Py_INCREF(PyExc_OSError);
3205 PosixError = PyExc_OSError;
3206 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003207}