blob: 1be268c258379c6e51e4181e8ef2eedbf98b8a06 [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 Rossuma1065681999-01-25 23:20:23 +0000108#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000109#define HAVE_EXECV 1
110#define HAVE_PIPE 1
111#define HAVE_POPEN 1
112#define HAVE_SYSTEM 1
113#else /* 16-bit Windows */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000114#endif /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000115#else /* all other compilers */
116/* Unix functions that the configure script doesn't check for */
117#define HAVE_EXECV 1
118#define HAVE_FORK 1
119#define HAVE_GETCWD 1
120#define HAVE_GETEGID 1
121#define HAVE_GETEUID 1
122#define HAVE_GETGID 1
123#define HAVE_GETPPID 1
124#define HAVE_GETUID 1
125#define HAVE_KILL 1
126#define HAVE_OPENDIR 1
127#define HAVE_PIPE 1
128#define HAVE_POPEN 1
129#define HAVE_SYSTEM 1
130#define HAVE_WAIT 1
Guido van Rossumd371ff11999-01-25 16:12:23 +0000131#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000132#endif /* _MSC_VER */
133#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000134#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000135#endif /* ! __IBMC__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000136
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000137#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000138
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139#ifdef HAVE_UNISTD_H
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000140#include <unistd.h>
Guido van Rossum36bc6801995-06-14 22:54:23 +0000141#endif
142
143#ifdef NeXT
144/* NeXT's <unistd.h> and <utime.h> aren't worth much */
145#undef HAVE_UNISTD_H
146#undef HAVE_UTIME_H
Guido van Rossumb9f866c1997-05-22 15:12:39 +0000147#define HAVE_WAITPID
Guido van Rossum36bc6801995-06-14 22:54:23 +0000148/* #undef HAVE_GETCWD */
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000149#define UNION_WAIT /* This should really be checked for by autoconf */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000150#endif
151
152#ifdef HAVE_UNISTD_H
Guido van Rossumad0ee831995-03-01 10:34:45 +0000153/* XXX These are for SunOS4.1.3 but shouldn't hurt elsewhere */
154extern int rename();
155extern int pclose();
156extern int lstat();
157extern int symlink();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000158#else /* !HAVE_UNISTD_H */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000159#if defined(PYCC_VACPP)
160extern int mkdir Py_PROTO((char *));
161#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000162#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Barry Warsaw53699e91996-12-10 23:23:01 +0000163extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000164#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000165extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000166#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000167#endif
168#if defined(__IBMC__) || defined(__IBMCPP__)
169extern int chdir Py_PROTO((char *));
170extern int rmdir Py_PROTO((char *));
171#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000172extern int chdir Py_PROTO((const char *));
173extern int rmdir Py_PROTO((const char *));
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000174#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000175extern int chmod Py_PROTO((const char *, mode_t));
176extern int chown Py_PROTO((const char *, uid_t, gid_t));
177extern char *getcwd Py_PROTO((char *, int));
178extern char *strerror Py_PROTO((int));
179extern int link Py_PROTO((const char *, const char *));
180extern int rename Py_PROTO((const char *, const char *));
181extern int stat Py_PROTO((const char *, struct stat *));
182extern int unlink Py_PROTO((const char *));
183extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000184#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000185extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000186#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000187#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000188extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000189#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000190#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000191
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000192#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000193
Guido van Rossumb6775db1994-08-01 11:34:53 +0000194#ifdef HAVE_UTIME_H
195#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000196#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000197
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000198#ifdef HAVE_SYS_UTIME_H
199#include <sys/utime.h>
200#define HAVE_UTIME_H /* pretend we do for the rest of this file */
201#endif /* HAVE_SYS_UTIME_H */
202
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203#ifdef HAVE_SYS_TIMES_H
204#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000205#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206
207#ifdef HAVE_SYS_PARAM_H
208#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000209#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000210
211#ifdef HAVE_SYS_UTSNAME_H
212#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000213#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000214
215#ifndef MAXPATHLEN
216#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000217#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000219#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000220#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000221#define NAMLEN(dirent) strlen((dirent)->d_name)
222#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000223#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000224#include <direct.h>
225#define NAMLEN(dirent) strlen((dirent)->d_name)
226#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000227#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000228#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000229#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000230#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000231#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000232#endif
233#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000235#endif
236#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000237#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000238#endif
239#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000241#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#include <direct.h>
243#include <io.h>
244#include <process.h>
245#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000246#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000247#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000248#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#else /* 16-bit Windows */
250#include <dos.h>
251#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000252#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000253#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
Guido van Rossumd48f2521997-12-05 22:19:34 +0000255#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000257#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000259#ifdef UNION_WAIT
260/* Emulate some macros on systems that have a union instead of macros */
261
262#ifndef WIFEXITED
263#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
264#endif
265
266#ifndef WEXITSTATUS
267#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
268#endif
269
270#ifndef WTERMSIG
271#define WTERMSIG(u_wait) ((u_wait).w_termsig)
272#endif
273
274#endif /* UNION_WAIT */
275
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000276/* Return a dictionary corresponding to the POSIX environment table */
277
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000278#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000280#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281
Barry Warsaw53699e91996-12-10 23:23:01 +0000282static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283convertenviron()
284{
Barry Warsaw53699e91996-12-10 23:23:01 +0000285 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000287 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000288 if (d == NULL)
289 return NULL;
290 if (environ == NULL)
291 return d;
292 /* XXX This part ignores errors */
293 for (e = environ; *e != NULL; e++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000294 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295 char *p = strchr(*e, '=');
296 if (p == NULL)
297 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000298 v = PyString_FromString(p+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000299 if (v == NULL)
300 continue;
301 *p = '\0';
Barry Warsaw53699e91996-12-10 23:23:01 +0000302 (void) PyDict_SetItemString(d, *e, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303 *p = '=';
Barry Warsaw53699e91996-12-10 23:23:01 +0000304 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000306#if defined(PYOS_OS2)
307 {
308 APIRET rc;
309 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
310
311 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
312 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
313 PyObject *v = PyString_FromString(buffer);
314 PyDict_SetItemString(d, "BEGINLIBPATH", v);
315 Py_DECREF(v);
316 }
317 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
318 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
319 PyObject *v = PyString_FromString(buffer);
320 PyDict_SetItemString(d, "ENDLIBPATH", v);
321 Py_DECREF(v);
322 }
323 }
324#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325 return d;
326}
327
328
Barry Warsaw53699e91996-12-10 23:23:01 +0000329static PyObject *PosixError; /* Exception posix.error */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000330
331/* Set a POSIX-specific error from errno, and return NULL */
332
Barry Warsawd58d7641998-07-23 16:14:40 +0000333static PyObject *
334posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000335{
Barry Warsaw53699e91996-12-10 23:23:01 +0000336 return PyErr_SetFromErrno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337}
Barry Warsawd58d7641998-07-23 16:14:40 +0000338static PyObject *
339posix_error_with_filename(name)
340 char* name;
341{
342 return PyErr_SetFromErrnoWithFilename(PosixError, name);
343}
344
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345
Guido van Rossumd48f2521997-12-05 22:19:34 +0000346#if defined(PYOS_OS2)
347/**********************************************************************
348 * Helper Function to Trim and Format OS/2 Messages
349 **********************************************************************/
350 static void
351os2_formatmsg(char *msgbuf, int msglen, char *reason)
352{
353 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
354
355 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
356 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
357
358 while (lastc > msgbuf && isspace(*lastc))
359 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
360 }
361
362 /* Add Optional Reason Text */
363 if (reason) {
364 strcat(msgbuf, " : ");
365 strcat(msgbuf, reason);
366 }
367}
368
369/**********************************************************************
370 * Decode an OS/2 Operating System Error Code
371 *
372 * A convenience function to lookup an OS/2 error code and return a
373 * text message we can use to raise a Python exception.
374 *
375 * Notes:
376 * The messages for errors returned from the OS/2 kernel reside in
377 * the file OSO001.MSG in the \OS2 directory hierarchy.
378 *
379 **********************************************************************/
380 static char *
381os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
382{
383 APIRET rc;
384 ULONG msglen;
385
386 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
387 Py_BEGIN_ALLOW_THREADS
388 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
389 errorcode, "oso001.msg", &msglen);
390 Py_END_ALLOW_THREADS
391
392 if (rc == NO_ERROR)
393 os2_formatmsg(msgbuf, msglen, reason);
394 else
395 sprintf(msgbuf, "unknown OS error #%d", errorcode);
396
397 return msgbuf;
398}
399
400/* Set an OS/2-specific error and return NULL. OS/2 kernel
401 errors are not in a global variable e.g. 'errno' nor are
402 they congruent with posix error numbers. */
403
404static PyObject * os2_error(int code)
405{
406 char text[1024];
407 PyObject *v;
408
409 os2_strerror(text, sizeof(text), code, "");
410
411 v = Py_BuildValue("(is)", code, text);
412 if (v != NULL) {
413 PyErr_SetObject(PosixError, v);
414 Py_DECREF(v);
415 }
416 return NULL; /* Signal to Python that an Exception is Pending */
417}
418
419#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420
421/* POSIX generic methods */
422
Barry Warsaw53699e91996-12-10 23:23:01 +0000423static PyObject *
Guido van Rossum21142a01999-01-08 21:05:37 +0000424posix_int(args, func)
425 PyObject *args;
426 int (*func) Py_FPROTO((int));
427{
428 int fd;
429 int res;
430 if (!PyArg_Parse(args, "i", &fd))
431 return NULL;
432 Py_BEGIN_ALLOW_THREADS
433 res = (*func)(fd);
434 Py_END_ALLOW_THREADS
435 if (res < 0)
436 return posix_error();
437 Py_INCREF(Py_None);
438 return Py_None;
439}
440
441
442static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000444 PyObject *args;
445 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000447 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000448 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000449 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000451 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000452 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000453 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000454 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000455 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000456 Py_INCREF(Py_None);
457 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458}
459
Barry Warsaw53699e91996-12-10 23:23:01 +0000460static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000462 PyObject *args;
463 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000465 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000466 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
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)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000471 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000472 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000473 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000475 Py_INCREF(Py_None);
476 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477}
478
Barry Warsaw53699e91996-12-10 23:23:01 +0000479static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000481 PyObject *args;
482 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000484 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000486 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000487 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000489 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000490 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000491 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000492 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000493 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000494 Py_INCREF(Py_None);
495 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496}
497
Barry Warsaw53699e91996-12-10 23:23:01 +0000498static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000499posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000500 PyObject *args;
501 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000502{
503 char *path;
504 int i,i2;
505 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000506 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000507 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000508 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000509 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000510 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000511 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000512 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000513 Py_INCREF(Py_None);
514 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000515}
516
Barry Warsaw53699e91996-12-10 23:23:01 +0000517static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000519 PyObject *self;
520 PyObject *args;
521 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000522{
523 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000524 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000525 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000526 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000528 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000529 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000530 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000531 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000532 return posix_error_with_filename(path);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000533#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000534 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000535 (long)st.st_mode,
536 (long)st.st_ino,
537 (long)st.st_dev,
538 (long)st.st_nlink,
539 (long)st.st_uid,
540 (long)st.st_gid,
541 (long)st.st_size,
542 (long)st.st_atime,
543 (long)st.st_mtime,
544 (long)st.st_ctime);
545#else
546 return Py_BuildValue("(lLllllLlll)",
547 (long)st.st_mode,
548 (LONG_LONG)st.st_ino,
549 (long)st.st_dev,
550 (long)st.st_nlink,
551 (long)st.st_uid,
552 (long)st.st_gid,
553 (LONG_LONG)st.st_size,
554 (long)st.st_atime,
555 (long)st.st_mtime,
556 (long)st.st_ctime);
557#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000558}
559
560
561/* POSIX methods */
562
Guido van Rossum94f6f721999-01-06 18:42:14 +0000563static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000564"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000565Test for access to a file.";
566
567static PyObject *
568posix_access(self, args)
569 PyObject *self;
570 PyObject *args;
571{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000572 char *path;
573 int mode;
574 int res;
575
576 if (!PyArg_Parse(args, "(si)", &path, &mode))
577 return NULL;
578 Py_BEGIN_ALLOW_THREADS
579 res = access(path, mode);
580 Py_END_ALLOW_THREADS
581 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000582}
583
Guido van Rossumd371ff11999-01-25 16:12:23 +0000584#ifndef F_OK
585#define F_OK 0
586#endif
587#ifndef R_OK
588#define R_OK 4
589#endif
590#ifndef W_OK
591#define W_OK 2
592#endif
593#ifndef X_OK
594#define X_OK 1
595#endif
596
597#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000598static char posix_ttyname__doc__[] =
599"ttyname(fd, mode) -> String\n\
600Return the name of the terminal device connected to 'fd'.";
601
602static PyObject *
603posix_ttyname(self, args)
604 PyObject *self;
605 PyObject *args;
606{
607 PyObject *file;
608 int id;
609 char *ret;
610
Guido van Rossum94f6f721999-01-06 18:42:14 +0000611 if (!PyArg_Parse(args, "i", &id))
612 return NULL;
613
Guido van Rossum94f6f721999-01-06 18:42:14 +0000614 ret = ttyname(id);
615 if (ret == NULL)
616 return(posix_error());
617 return(PyString_FromString(ret));
618}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000619#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000620
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000621static char posix_chdir__doc__[] =
622"chdir(path) -> None\n\
623Change the current working directory to the specified path.";
624
Barry Warsaw53699e91996-12-10 23:23:01 +0000625static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000627 PyObject *self;
628 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630 return posix_1str(args, chdir);
631}
632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000633
634static char posix_chmod__doc__[] =
635"chmod(path, mode) -> None\n\
636Change the access permissions of a file.";
637
Barry Warsaw53699e91996-12-10 23:23:01 +0000638static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 PyObject *self;
641 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643 return posix_strint(args, chmod);
644}
645
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000646
Guido van Rossum21142a01999-01-08 21:05:37 +0000647#ifdef HAVE_FSYNC
648static char posix_fsync__doc__[] =
649"fsync(fildes) -> None\n\
650force write of file with filedescriptor to disk.";
651
652static PyObject *
653posix_fsync(self, args)
654 PyObject *self;
655 PyObject *args;
656{
Guido van Rossum21142a01999-01-08 21:05:37 +0000657 return posix_int(args, fsync);
658}
659#endif /* HAVE_FSYNC */
660
661#ifdef HAVE_FDATASYNC
662static char posix_fdatasync__doc__[] =
663"fdatasync(fildes) -> None\n\
664force write of file with filedescriptor to disk.\n\
665 does not force update of metadata.";
666
Guido van Rossum5d00b6d1999-01-08 21:28:05 +0000667extern int fdatasync(int); /* Prototype just in case */
668
Guido van Rossum21142a01999-01-08 21:05:37 +0000669static PyObject *
670posix_fdatasync(self, args)
671 PyObject *self;
672 PyObject *args;
673{
Guido van Rossum21142a01999-01-08 21:05:37 +0000674 return posix_int(args, fdatasync);
675}
676#endif /* HAVE_FDATASYNC */
677
678
Guido van Rossumb6775db1994-08-01 11:34:53 +0000679#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000680static char posix_chown__doc__[] =
681"chown(path, uid, gid) -> None\n\
682Change the owner and group id of path to the numeric uid and gid.";
683
Barry Warsaw53699e91996-12-10 23:23:01 +0000684static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000685posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000686 PyObject *self;
687 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000688{
689 return posix_strintint(args, chown);
690}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000691#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000692
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000693
Guido van Rossum36bc6801995-06-14 22:54:23 +0000694#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000695static char posix_getcwd__doc__[] =
696"getcwd() -> path\n\
697Return a string representing the current working directory.";
698
Barry Warsaw53699e91996-12-10 23:23:01 +0000699static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000700posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000701 PyObject *self;
702 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703{
704 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000705 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000706 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000707 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000708 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000709 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000710 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000711 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000712 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000713 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000715#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000717
Guido van Rossumb6775db1994-08-01 11:34:53 +0000718#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000719static char posix_link__doc__[] =
720"link(src, dst) -> None\n\
721Create a hard link to a file.";
722
Barry Warsaw53699e91996-12-10 23:23:01 +0000723static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000724posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000725 PyObject *self;
726 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000728 return posix_2str(args, link);
729}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000730#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000732
733static char posix_listdir__doc__[] =
734"listdir(path) -> list_of_strings\n\
735Return a list containing the names of the entries in the directory.\n\
736\n\
737 path: path of directory to list\n\
738\n\
739The list is in arbitrary order. It does not include the special\n\
740entries '.' and '..' even if they are present in the directory.";
741
Barry Warsaw53699e91996-12-10 23:23:01 +0000742static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000743posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000744 PyObject *self;
745 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000746{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000747 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000748 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000749#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000750
Guido van Rossumb6775db1994-08-01 11:34:53 +0000751 char *name;
752 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000753 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000754 HANDLE hFindFile;
755 WIN32_FIND_DATA FileData;
756 char namebuf[MAX_PATH+5];
757
Guido van Rossum7e488981998-10-08 02:25:24 +0000758 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000759 return NULL;
760 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000761 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000762 return NULL;
763 }
764 strcpy(namebuf, name);
765 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
766 namebuf[len++] = '/';
767 strcpy(namebuf + len, "*.*");
768
Barry Warsaw53699e91996-12-10 23:23:01 +0000769 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000770 return NULL;
771
772 hFindFile = FindFirstFile(namebuf, &FileData);
773 if (hFindFile == INVALID_HANDLE_VALUE) {
774 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000775 if (errno == ERROR_FILE_NOT_FOUND)
776 return PyList_New(0);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000777 return posix_error();
778 }
779 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000780 if (FileData.cFileName[0] == '.' &&
781 (FileData.cFileName[1] == '\0' ||
782 FileData.cFileName[1] == '.' &&
783 FileData.cFileName[2] == '\0'))
784 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000785 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000786 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000787 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788 d = NULL;
789 break;
790 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000791 if (PyList_Append(d, v) != 0) {
792 Py_DECREF(v);
793 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000794 d = NULL;
795 break;
796 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000797 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798 } while (FindNextFile(hFindFile, &FileData) == TRUE);
799
800 if (FindClose(hFindFile) == FALSE) {
801 errno = GetLastError();
802 return posix_error();
803 }
804
805 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000806
Guido van Rossum8d665e61996-06-26 18:22:49 +0000807#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000808#ifdef _MSC_VER /* 16-bit Windows */
809
810#ifndef MAX_PATH
811#define MAX_PATH 250
812#endif
813 char *name, *pt;
814 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000815 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000816 char namebuf[MAX_PATH+5];
817 struct _find_t ep;
818
Guido van Rossum7e488981998-10-08 02:25:24 +0000819 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000820 return NULL;
821 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000822 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000823 return NULL;
824 }
825 strcpy(namebuf, name);
826 for (pt = namebuf; *pt; pt++)
827 if (*pt == '/')
828 *pt = '\\';
829 if (namebuf[len-1] != '\\')
830 namebuf[len++] = '\\';
831 strcpy(namebuf + len, "*.*");
832
Barry Warsaw53699e91996-12-10 23:23:01 +0000833 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000834 return NULL;
835
836 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000837 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
838 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000839 errno = ENOENT;
840 return posix_error();
841 }
842 do {
843 if (ep.name[0] == '.' &&
844 (ep.name[1] == '\0' ||
845 ep.name[1] == '.' &&
846 ep.name[2] == '\0'))
847 continue;
848 strcpy(namebuf, ep.name);
849 for (pt = namebuf; *pt; pt++)
850 if (isupper(*pt))
851 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000852 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000853 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000854 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000855 d = NULL;
856 break;
857 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000858 if (PyList_Append(d, v) != 0) {
859 Py_DECREF(v);
860 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000861 d = NULL;
862 break;
863 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000864 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000865 } while (_dos_findnext(&ep) == 0);
866
867 return d;
868
869#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000870#if defined(PYOS_OS2)
871
872#ifndef MAX_PATH
873#define MAX_PATH CCHMAXPATH
874#endif
875 char *name, *pt;
876 int len;
877 PyObject *d, *v;
878 char namebuf[MAX_PATH+5];
879 HDIR hdir = 1;
880 ULONG srchcnt = 1;
881 FILEFINDBUF3 ep;
882 APIRET rc;
883
Guido van Rossum7e488981998-10-08 02:25:24 +0000884 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000885 return NULL;
886 if (len >= MAX_PATH) {
887 PyErr_SetString(PyExc_ValueError, "path too long");
888 return NULL;
889 }
890 strcpy(namebuf, name);
891 for (pt = namebuf; *pt; pt++)
892 if (*pt == '/')
893 *pt = '\\';
894 if (namebuf[len-1] != '\\')
895 namebuf[len++] = '\\';
896 strcpy(namebuf + len, "*.*");
897
898 if ((d = PyList_New(0)) == NULL)
899 return NULL;
900
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000901 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
902 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000903 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000904 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
905 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
906 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000907
908 if (rc != NO_ERROR) {
909 errno = ENOENT;
910 return posix_error();
911 }
912
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000913 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000914 do {
915 if (ep.achName[0] == '.'
916 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000917 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000918
919 strcpy(namebuf, ep.achName);
920
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000921 /* Leave Case of Name Alone -- In Native Form */
922 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000923
924 v = PyString_FromString(namebuf);
925 if (v == NULL) {
926 Py_DECREF(d);
927 d = NULL;
928 break;
929 }
930 if (PyList_Append(d, v) != 0) {
931 Py_DECREF(v);
932 Py_DECREF(d);
933 d = NULL;
934 break;
935 }
936 Py_DECREF(v);
937 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
938 }
939
940 return d;
941#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000942
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000943 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000944 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000945 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000946 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000947 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000948 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000949 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000950 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000951 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000952 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000953 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000954 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000955 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000956 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000957 return NULL;
958 }
959 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000960 if (ep->d_name[0] == '.' &&
961 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000962 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000963 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000964 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000966 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967 d = NULL;
968 break;
969 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000970 if (PyList_Append(d, v) != 0) {
971 Py_DECREF(v);
972 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000973 d = NULL;
974 break;
975 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000976 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000977 }
978 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000979 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000980
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000981 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000982
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000983#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000984#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000985#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000986}
987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000988static char posix_mkdir__doc__[] =
989"mkdir(path [, mode=0777]) -> None\n\
990Create a directory.";
991
Barry Warsaw53699e91996-12-10 23:23:01 +0000992static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000993posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000994 PyObject *self;
995 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000996{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000997 int res;
998 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000999 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +00001000 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001001 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001002 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001003#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001004 res = mkdir(path);
1005#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001006 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001007#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001008 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001009 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001010 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001011 Py_INCREF(Py_None);
1012 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001013}
1014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001015
Guido van Rossumb6775db1994-08-01 11:34:53 +00001016#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001017static char posix_nice__doc__[] =
1018"nice(inc) -> new_priority\n\
1019Decrease the priority of process and return new priority.";
1020
Barry Warsaw53699e91996-12-10 23:23:01 +00001021static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +00001022posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001023 PyObject *self;
1024 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001025{
1026 int increment, value;
1027
Barry Warsaw53699e91996-12-10 23:23:01 +00001028 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001029 return NULL;
1030 value = nice(increment);
1031 if (value == -1)
1032 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001033 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001034}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001035#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001036
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001037
1038static char posix_rename__doc__[] =
1039"rename(old, new) -> None\n\
1040Rename a file or directory.";
1041
Barry Warsaw53699e91996-12-10 23:23:01 +00001042static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001043posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001044 PyObject *self;
1045 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001046{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001047 return posix_2str(args, rename);
1048}
1049
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001050
1051static char posix_rmdir__doc__[] =
1052"rmdir(path) -> None\n\
1053Remove a directory.";
1054
Barry Warsaw53699e91996-12-10 23:23:01 +00001055static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001056posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001057 PyObject *self;
1058 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001059{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001060 return posix_1str(args, rmdir);
1061}
1062
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001063
1064static char posix_stat__doc__[] =
1065"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1066Perform a stat system call on the given path.";
1067
Barry Warsaw53699e91996-12-10 23:23:01 +00001068static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001069posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001070 PyObject *self;
1071 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001072{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001073 return posix_do_stat(self, args, stat);
1074}
1075
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001076
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001077#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001078static char posix_system__doc__[] =
1079"system(command) -> exit_status\n\
1080Execute the command (a string) in a subshell.";
1081
Barry Warsaw53699e91996-12-10 23:23:01 +00001082static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001083posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001084 PyObject *self;
1085 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001086{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001087 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001088 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001089 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001090 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001091 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001092 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001093 Py_END_ALLOW_THREADS
1094 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001095}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001096#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001098
1099static char posix_umask__doc__[] =
1100"umask(new_mask) -> old_mask\n\
1101Set the current numeric umask and return the previous umask.";
1102
Barry Warsaw53699e91996-12-10 23:23:01 +00001103static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001104posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001105 PyObject *self;
1106 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001107{
1108 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +00001109 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001110 return NULL;
1111 i = umask(i);
1112 if (i < 0)
1113 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001114 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001115}
1116
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001117
1118static char posix_unlink__doc__[] =
1119"unlink(path) -> None\n\
1120Remove a file (same as remove(path)).";
1121
1122static char posix_remove__doc__[] =
1123"remove(path) -> None\n\
1124Remove a file (same as unlink(path)).";
1125
Barry Warsaw53699e91996-12-10 23:23:01 +00001126static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001127posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001128 PyObject *self;
1129 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001130{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001131 return posix_1str(args, unlink);
1132}
1133
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001134
Guido van Rossumb6775db1994-08-01 11:34:53 +00001135#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001136static char posix_uname__doc__[] =
1137"uname() -> (sysname, nodename, release, version, machine)\n\
1138Return a tuple identifying the current operating system.";
1139
Barry Warsaw53699e91996-12-10 23:23:01 +00001140static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001141posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001142 PyObject *self;
1143 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001144{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001145 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001146 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001147 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001148 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001149 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001150 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001151 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001152 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001153 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001154 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001155 u.sysname,
1156 u.nodename,
1157 u.release,
1158 u.version,
1159 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001160}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001161#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001163
1164static char posix_utime__doc__[] =
1165"utime(path, (atime, utime)) -> None\n\
1166Set the access and modified time of the file to the given values.";
1167
Barry Warsaw53699e91996-12-10 23:23:01 +00001168static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001169posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001170 PyObject *self;
1171 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001173 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001174 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001175 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001176
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001177/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001178#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001179 struct utimbuf buf;
1180#define ATIME buf.actime
1181#define MTIME buf.modtime
1182#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001183#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001184 time_t buf[2];
1185#define ATIME buf[0]
1186#define MTIME buf[1]
1187#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001188#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001189
Barry Warsaw53699e91996-12-10 23:23:01 +00001190 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001191 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001192 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001193 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001194 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001195 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001196 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001197 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001198 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001199 Py_INCREF(Py_None);
1200 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001201#undef UTIME_ARG
1202#undef ATIME
1203#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001204}
1205
Guido van Rossum85e3b011991-06-03 12:42:10 +00001206
Guido van Rossum3b066191991-06-04 19:40:25 +00001207/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001208
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001209static char posix__exit__doc__[] =
1210"_exit(status)\n\
1211Exit to the system with specified status, without normal exit processing.";
1212
Barry Warsaw53699e91996-12-10 23:23:01 +00001213static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001214posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001215 PyObject *self;
1216 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001217{
1218 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001219 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001220 return NULL;
1221 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001222 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001223}
1224
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001225
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001226#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001227static char posix_execv__doc__[] =
1228"execv(path, args)\n\
1229Execute an executable path with arguments, replacing current process.\n\
1230\n\
1231 path: path of executable file\n\
1232 args: tuple or list of strings";
1233
Barry Warsaw53699e91996-12-10 23:23:01 +00001234static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001235posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001236 PyObject *self;
1237 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001238{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001239 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001240 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001241 char **argvlist;
1242 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001243 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001244
Guido van Rossum89b33251993-10-22 14:26:06 +00001245 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001246 argv is a list or tuple of strings. */
1247
Barry Warsaw53699e91996-12-10 23:23:01 +00001248 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001249 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001250 if (PyList_Check(argv)) {
1251 argc = PyList_Size(argv);
1252 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001253 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001254 else if (PyTuple_Check(argv)) {
1255 argc = PyTuple_Size(argv);
1256 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001257 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001258 else {
1259 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001260 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001261 return NULL;
1262 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001263
Barry Warsaw53699e91996-12-10 23:23:01 +00001264 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001265 if (argvlist == NULL)
1266 return NULL;
1267 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001268 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1269 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001270 goto badarg;
1271 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001272 }
1273 argvlist[argc] = NULL;
1274
Guido van Rossumb6775db1994-08-01 11:34:53 +00001275#ifdef BAD_EXEC_PROTOTYPES
1276 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001277#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001278 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001279#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001280
Guido van Rossum85e3b011991-06-03 12:42:10 +00001281 /* If we get here it's definitely an error */
1282
Barry Warsaw53699e91996-12-10 23:23:01 +00001283 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001284 return posix_error();
1285}
1286
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001287
1288static char posix_execve__doc__[] =
1289"execve(path, args, env)\n\
1290Execute a path with arguments and environment, replacing current process.\n\
1291\n\
1292 path: path of executable file\n\
1293 args: tuple or list of arguments\n\
1294 env: dictonary of strings mapping to strings";
1295
Barry Warsaw53699e91996-12-10 23:23:01 +00001296static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001297posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001298 PyObject *self;
1299 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001300{
1301 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001302 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001303 char **argvlist;
1304 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001305 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001306 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001307 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001308
1309 /* execve has three arguments: (path, argv, env), where
1310 argv is a list or tuple of strings and env is a dictionary
1311 like posix.environ. */
1312
Barry Warsaw53699e91996-12-10 23:23:01 +00001313 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001314 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001315 if (PyList_Check(argv)) {
1316 argc = PyList_Size(argv);
1317 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001318 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001319 else if (PyTuple_Check(argv)) {
1320 argc = PyTuple_Size(argv);
1321 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001322 }
1323 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001324 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001325 return NULL;
1326 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001327 if (!PyMapping_Check(env)) {
1328 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001329 return NULL;
1330 }
1331
Barry Warsaw53699e91996-12-10 23:23:01 +00001332 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001333 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001334 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001335 return NULL;
1336 }
1337 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001338 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001339 "s;argv must be list of strings",
1340 &argvlist[i]))
1341 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001342 goto fail_1;
1343 }
1344 }
1345 argvlist[argc] = NULL;
1346
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001347 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001348 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001349 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001350 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001351 goto fail_1;
1352 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001353 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001354 keys = PyMapping_Keys(env);
1355 vals = PyMapping_Values(env);
1356 if (!keys || !vals)
1357 goto fail_2;
1358
1359 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001360 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001361
1362 key = PyList_GetItem(keys, pos);
1363 val = PyList_GetItem(vals, pos);
1364 if (!key || !val)
1365 goto fail_2;
1366
Barry Warsaw53699e91996-12-10 23:23:01 +00001367 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001368 !PyArg_Parse(val, "s;non-string value in env", &v))
1369 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001370 goto fail_2;
1371 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001372
1373#if defined(PYOS_OS2)
1374 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1375 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1376#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001377 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001378 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001379 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001380 goto fail_2;
1381 }
1382 sprintf(p, "%s=%s", k, v);
1383 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001384#if defined(PYOS_OS2)
1385 }
1386#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001387 }
1388 envlist[envc] = 0;
1389
Guido van Rossumb6775db1994-08-01 11:34:53 +00001390
1391#ifdef BAD_EXEC_PROTOTYPES
1392 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001393#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001394 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001395#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001396
1397 /* If we get here it's definitely an error */
1398
1399 (void) posix_error();
1400
1401 fail_2:
1402 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001403 PyMem_DEL(envlist[envc]);
1404 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001405 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001406 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001407 Py_XDECREF(vals);
1408 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001409 return NULL;
1410}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001411#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001412
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001413
Guido van Rossuma1065681999-01-25 23:20:23 +00001414#ifdef HAVE_SPAWNV
1415static char posix_spawnv__doc__[] =
1416"spawnv(path, args)\n\
1417Execute an executable path with arguments, replacing current process.\n\
1418\n\
1419 path: path of executable file\n\
1420 args: tuple or list of strings";
1421
1422static PyObject *
1423posix_spawnv(self, args)
1424 PyObject *self;
1425 PyObject *args;
1426{
1427 char *path;
1428 PyObject *argv;
1429 char **argvlist;
1430 int mode, i, argc;
1431 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1432
1433 /* spawnv has three arguments: (mode, path, argv), where
1434 argv is a list or tuple of strings. */
1435
1436 if (!PyArg_Parse(args, "(isO)", &mode, &path, &argv))
1437 return NULL;
1438 if (PyList_Check(argv)) {
1439 argc = PyList_Size(argv);
1440 getitem = PyList_GetItem;
1441 }
1442 else if (PyTuple_Check(argv)) {
1443 argc = PyTuple_Size(argv);
1444 getitem = PyTuple_GetItem;
1445 }
1446 else {
1447 badarg:
1448 PyErr_BadArgument();
1449 return NULL;
1450 }
1451
1452 argvlist = PyMem_NEW(char *, argc+1);
1453 if (argvlist == NULL)
1454 return NULL;
1455 for (i = 0; i < argc; i++) {
1456 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1457 PyMem_DEL(argvlist);
1458 goto badarg;
1459 }
1460 }
1461 argvlist[argc] = NULL;
1462
1463 i = _spawnv(mode, path, argvlist);
1464
1465 PyMem_DEL(argvlist);
1466
1467 if (i == -1)
1468 return posix_error();
1469 else
1470 return Py_BuildValue("i", i);
1471}
1472
1473
1474static char posix_spawnve__doc__[] =
1475"spawnve(path, args, env)\n\
1476Execute a path with arguments and environment, replacing current process.\n\
1477\n\
1478 path: path of executable file\n\
1479 args: tuple or list of arguments\n\
1480 env: dictonary of strings mapping to strings";
1481
1482static PyObject *
1483posix_spawnve(self, args)
1484 PyObject *self;
1485 PyObject *args;
1486{
1487 char *path;
1488 PyObject *argv, *env;
1489 char **argvlist;
1490 char **envlist;
1491 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1492 int mode, i, pos, argc, envc;
1493 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1494
1495 /* spawnve has four arguments: (mode, path, argv, env), where
1496 argv is a list or tuple of strings and env is a dictionary
1497 like posix.environ. */
1498
1499 if (!PyArg_Parse(args, "(isOO)", &mode, &path, &argv, &env))
1500 return NULL;
1501 if (PyList_Check(argv)) {
1502 argc = PyList_Size(argv);
1503 getitem = PyList_GetItem;
1504 }
1505 else if (PyTuple_Check(argv)) {
1506 argc = PyTuple_Size(argv);
1507 getitem = PyTuple_GetItem;
1508 }
1509 else {
1510 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1511 return NULL;
1512 }
1513 if (!PyMapping_Check(env)) {
1514 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1515 return NULL;
1516 }
1517
1518 argvlist = PyMem_NEW(char *, argc+1);
1519 if (argvlist == NULL) {
1520 PyErr_NoMemory();
1521 return NULL;
1522 }
1523 for (i = 0; i < argc; i++) {
1524 if (!PyArg_Parse((*getitem)(argv, i),
1525 "s;argv must be list of strings",
1526 &argvlist[i]))
1527 {
1528 goto fail_1;
1529 }
1530 }
1531 argvlist[argc] = NULL;
1532
1533 i = PyMapping_Length(env);
1534 envlist = PyMem_NEW(char *, i + 1);
1535 if (envlist == NULL) {
1536 PyErr_NoMemory();
1537 goto fail_1;
1538 }
1539 envc = 0;
1540 keys = PyMapping_Keys(env);
1541 vals = PyMapping_Values(env);
1542 if (!keys || !vals)
1543 goto fail_2;
1544
1545 for (pos = 0; pos < i; pos++) {
1546 char *p, *k, *v;
1547
1548 key = PyList_GetItem(keys, pos);
1549 val = PyList_GetItem(vals, pos);
1550 if (!key || !val)
1551 goto fail_2;
1552
1553 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1554 !PyArg_Parse(val, "s;non-string value in env", &v))
1555 {
1556 goto fail_2;
1557 }
1558 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1559 if (p == NULL) {
1560 PyErr_NoMemory();
1561 goto fail_2;
1562 }
1563 sprintf(p, "%s=%s", k, v);
1564 envlist[envc++] = p;
1565 }
1566 envlist[envc] = 0;
1567
1568 i = _spawnve(mode, path, argvlist, envlist);
1569 if (i == -1)
1570 (void) posix_error();
1571 else
1572 res = Py_BuildValue("i", i);
1573
1574 fail_2:
1575 while (--envc >= 0)
1576 PyMem_DEL(envlist[envc]);
1577 PyMem_DEL(envlist);
1578 fail_1:
1579 PyMem_DEL(argvlist);
1580 Py_XDECREF(vals);
1581 Py_XDECREF(keys);
1582 return res;
1583}
1584#endif /* HAVE_SPAWNV */
1585
1586
Guido van Rossumad0ee831995-03-01 10:34:45 +00001587#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001588static char posix_fork__doc__[] =
1589"fork() -> pid\n\
1590Fork a child process.\n\
1591\n\
1592Return 0 to child process and PID of child to parent process.";
1593
Barry Warsaw53699e91996-12-10 23:23:01 +00001594static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001595posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001596 PyObject *self;
1597 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001598{
1599 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001600 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001601 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001602 pid = fork();
1603 if (pid == -1)
1604 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001605 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001606 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001607}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001608#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001609
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001610
Guido van Rossumad0ee831995-03-01 10:34:45 +00001611#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001612static char posix_getegid__doc__[] =
1613"getegid() -> egid\n\
1614Return the current process's effective group id.";
1615
Barry Warsaw53699e91996-12-10 23:23:01 +00001616static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001617posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001618 PyObject *self;
1619 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001620{
Barry Warsaw53699e91996-12-10 23:23:01 +00001621 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001622 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001623 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001624}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001625#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001627
Guido van Rossumad0ee831995-03-01 10:34:45 +00001628#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001629static char posix_geteuid__doc__[] =
1630"geteuid() -> euid\n\
1631Return the current process's effective user id.";
1632
Barry Warsaw53699e91996-12-10 23:23:01 +00001633static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001634posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001635 PyObject *self;
1636 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001637{
Barry Warsaw53699e91996-12-10 23:23:01 +00001638 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001639 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001640 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001641}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001642#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001643
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001644
Guido van Rossumad0ee831995-03-01 10:34:45 +00001645#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001646static char posix_getgid__doc__[] =
1647"getgid() -> gid\n\
1648Return the current process's group id.";
1649
Barry Warsaw53699e91996-12-10 23:23:01 +00001650static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001651posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001652 PyObject *self;
1653 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001654{
Barry Warsaw53699e91996-12-10 23:23:01 +00001655 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001656 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001657 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001658}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001659#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001660
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001661
1662static char posix_getpid__doc__[] =
1663"getpid() -> pid\n\
1664Return the current process id";
1665
Barry Warsaw53699e91996-12-10 23:23:01 +00001666static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001667posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001668 PyObject *self;
1669 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001670{
Barry Warsaw53699e91996-12-10 23:23:01 +00001671 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001672 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001673 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001674}
1675
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001676
Guido van Rossumb6775db1994-08-01 11:34:53 +00001677#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001678static char posix_getpgrp__doc__[] =
1679"getpgrp() -> pgrp\n\
1680Return the current process group id.";
1681
Barry Warsaw53699e91996-12-10 23:23:01 +00001682static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001683posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001684 PyObject *self;
1685 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001686{
Barry Warsaw53699e91996-12-10 23:23:01 +00001687 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001688 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001689#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001690 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001691#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001692 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001693#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001694}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001695#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001696
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001697
Guido van Rossumb6775db1994-08-01 11:34:53 +00001698#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001699static char posix_setpgrp__doc__[] =
1700"setpgrp() -> None\n\
1701Make this process a session leader.";
1702
Barry Warsaw53699e91996-12-10 23:23:01 +00001703static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001704posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001705 PyObject *self;
1706 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001707{
Barry Warsaw53699e91996-12-10 23:23:01 +00001708 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001709 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001710#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001711 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001712#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001713 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001714#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001715 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001716 Py_INCREF(Py_None);
1717 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001718}
1719
Guido van Rossumb6775db1994-08-01 11:34:53 +00001720#endif /* HAVE_SETPGRP */
1721
Guido van Rossumad0ee831995-03-01 10:34:45 +00001722#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001723static char posix_getppid__doc__[] =
1724"getppid() -> ppid\n\
1725Return the parent's process id.";
1726
Barry Warsaw53699e91996-12-10 23:23:01 +00001727static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001728posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001729 PyObject *self;
1730 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001731{
Barry Warsaw53699e91996-12-10 23:23:01 +00001732 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001733 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001734 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001735}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001736#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001737
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001738
Guido van Rossumad0ee831995-03-01 10:34:45 +00001739#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001740static char posix_getuid__doc__[] =
1741"getuid() -> uid\n\
1742Return the current process's user id.";
1743
Barry Warsaw53699e91996-12-10 23:23:01 +00001744static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001745posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001746 PyObject *self;
1747 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001748{
Barry Warsaw53699e91996-12-10 23:23:01 +00001749 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001750 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001751 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001752}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001753#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001754
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001755
Guido van Rossumad0ee831995-03-01 10:34:45 +00001756#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001757static char posix_kill__doc__[] =
1758"kill(pid, sig) -> None\n\
1759Kill a process with a signal.";
1760
Barry Warsaw53699e91996-12-10 23:23:01 +00001761static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001762posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001763 PyObject *self;
1764 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001765{
1766 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001767 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001768 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001769#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001770 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1771 APIRET rc;
1772 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001773 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001774
1775 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1776 APIRET rc;
1777 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001778 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001779
1780 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001781 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001782#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001783 if (kill(pid, sig) == -1)
1784 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001785#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001786 Py_INCREF(Py_None);
1787 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001788}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001789#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001790
Guido van Rossumc0125471996-06-28 18:55:32 +00001791#ifdef HAVE_PLOCK
1792
1793#ifdef HAVE_SYS_LOCK_H
1794#include <sys/lock.h>
1795#endif
1796
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001797static char posix_plock__doc__[] =
1798"plock(op) -> None\n\
1799Lock program segments into memory.";
1800
Barry Warsaw53699e91996-12-10 23:23:01 +00001801static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001802posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001803 PyObject *self;
1804 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001805{
1806 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001807 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001808 return NULL;
1809 if (plock(op) == -1)
1810 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001811 Py_INCREF(Py_None);
1812 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001813}
1814#endif
1815
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001816
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001817#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001818static char posix_popen__doc__[] =
1819"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1820Open a pipe to/from a command returning a file object.";
1821
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001822#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001823static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001824async_system(const char *command)
1825{
1826 char *p, errormsg[256], args[1024];
1827 RESULTCODES rcodes;
1828 APIRET rc;
1829 char *shell = getenv("COMSPEC");
1830 if (!shell)
1831 shell = "cmd";
1832
1833 strcpy(args, shell);
1834 p = &args[ strlen(args)+1 ];
1835 strcpy(p, "/c ");
1836 strcat(p, command);
1837 p += strlen(p) + 1;
1838 *p = '\0';
1839
1840 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001841 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001842 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001843 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001844 &rcodes, shell);
1845 return rc;
1846}
1847
Guido van Rossumd48f2521997-12-05 22:19:34 +00001848static FILE *
1849popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001850{
1851 HFILE rhan, whan;
1852 FILE *retfd = NULL;
1853 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1854
Guido van Rossumd48f2521997-12-05 22:19:34 +00001855 if (rc != NO_ERROR) {
1856 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001857 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001858 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001859
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001860 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1861 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001862
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001863 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1864 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001865
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001866 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1867 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001868
1869 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001870 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001871 }
1872
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001873 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1874 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001875
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001876 close(oldfd); /* And Close Saved STDOUT Handle */
1877 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001878
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001879 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1880 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001881
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001882 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1883 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001884
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001885 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1886 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001887
1888 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001889 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001890 }
1891
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001892 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1893 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001894
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001895 close(oldfd); /* And Close Saved STDIN Handle */
1896 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001897
Guido van Rossumd48f2521997-12-05 22:19:34 +00001898 } else {
1899 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001900 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001901 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001902}
1903
1904static PyObject *
1905posix_popen(self, args)
1906 PyObject *self;
1907 PyObject *args;
1908{
1909 char *name;
1910 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001911 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001912 FILE *fp;
1913 PyObject *f;
1914 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1915 return NULL;
1916 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001917 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001918 Py_END_ALLOW_THREADS
1919 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001920 return os2_error(err);
1921
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001922 f = PyFile_FromFile(fp, name, mode, fclose);
1923 if (f != NULL)
1924 PyFile_SetBufSize(f, bufsize);
1925 return f;
1926}
1927
1928#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001929static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001930posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001931 PyObject *self;
1932 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001933{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001934 char *name;
1935 char *mode = "r";
1936 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001937 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001938 PyObject *f;
1939 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001940 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001941 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001942 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001943 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001944 if (fp == NULL)
1945 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001946 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001947 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001948 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001949 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001950}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001951#endif
1952
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001953#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001954
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001955
Guido van Rossumb6775db1994-08-01 11:34:53 +00001956#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001957static char posix_setuid__doc__[] =
1958"setuid(uid) -> None\n\
1959Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001960static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001961posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001962 PyObject *self;
1963 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001964{
1965 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001966 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001967 return NULL;
1968 if (setuid(uid) < 0)
1969 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001970 Py_INCREF(Py_None);
1971 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001972}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001973#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001974
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001975
Guido van Rossumb6775db1994-08-01 11:34:53 +00001976#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001977static char posix_setgid__doc__[] =
1978"setgid(gid) -> None\n\
1979Set the current process's group id.";
1980
Barry Warsaw53699e91996-12-10 23:23:01 +00001981static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001982posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001983 PyObject *self;
1984 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001985{
1986 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001987 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001988 return NULL;
1989 if (setgid(gid) < 0)
1990 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001991 Py_INCREF(Py_None);
1992 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001993}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001994#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001995
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001996
Guido van Rossumb6775db1994-08-01 11:34:53 +00001997#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001998static char posix_waitpid__doc__[] =
1999"waitpid(pid, options) -> (pid, status)\n\
2000Wait for completion of a give child process.";
2001
Barry Warsaw53699e91996-12-10 23:23:01 +00002002static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002003posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002004 PyObject *self;
2005 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002006{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002007 int pid, options;
2008#ifdef UNION_WAIT
2009 union wait status;
2010#define status_i (status.w_status)
2011#else
2012 int status;
2013#define status_i status
2014#endif
2015 status_i = 0;
2016
Barry Warsaw53699e91996-12-10 23:23:01 +00002017 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002018 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002019 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002020#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002021 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002022#else
2023 pid = waitpid(pid, &status, options);
2024#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002025 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002026 if (pid == -1)
2027 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002028 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002029 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002030}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002031#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002032
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002033
Guido van Rossumad0ee831995-03-01 10:34:45 +00002034#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002035static char posix_wait__doc__[] =
2036"wait() -> (pid, status)\n\
2037Wait for completion of a child process.";
2038
Barry Warsaw53699e91996-12-10 23:23:01 +00002039static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002040posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002041 PyObject *self;
2042 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00002043{
2044 int pid, sts;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002045#ifdef UNION_WAIT
2046 union wait status;
2047#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002048#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002049 int status;
2050#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002051#endif
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002052 status_i = 0;
2053 Py_BEGIN_ALLOW_THREADS
2054 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00002055 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00002056 if (pid == -1)
2057 return posix_error();
2058 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002059 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002060}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002061#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002062
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002063
2064static char posix_lstat__doc__[] =
2065"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
2066Like stat(path), but do not follow symbolic links.";
2067
Barry Warsaw53699e91996-12-10 23:23:01 +00002068static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002069posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002070 PyObject *self;
2071 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002072{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002073#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002074 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002075#else /* !HAVE_LSTAT */
2076 return posix_do_stat(self, args, stat);
2077#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002078}
2079
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002080
Guido van Rossumb6775db1994-08-01 11:34:53 +00002081#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002082static char posix_readlink__doc__[] =
2083"readlink(path) -> path\n\
2084Return a string representing the path to which the symbolic link points.";
2085
Barry Warsaw53699e91996-12-10 23:23:01 +00002086static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002087posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002088 PyObject *self;
2089 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002090{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002091 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002092 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002093 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002094 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002095 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002096 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00002097 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00002098 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002099 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002100 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002101 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002102}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002103#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002104
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002105
Guido van Rossumb6775db1994-08-01 11:34:53 +00002106#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002107static char posix_symlink__doc__[] =
2108"symlink(src, dst) -> None\n\
2109Create a symbolic link.";
2110
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002111static PyObject *
2112posix_symlink(self, args)
2113 PyObject *self;
2114 PyObject *args;
2115{
2116 return posix_2str(args, symlink);
2117}
2118#endif /* HAVE_SYMLINK */
2119
2120
2121#ifdef HAVE_TIMES
2122#ifndef HZ
2123#define HZ 60 /* Universal constant :-) */
2124#endif /* HZ */
2125
Guido van Rossumd48f2521997-12-05 22:19:34 +00002126#if defined(PYCC_VACPP) && defined(PYOS_OS2)
2127static long
2128system_uptime()
2129{
2130 ULONG value = 0;
2131
2132 Py_BEGIN_ALLOW_THREADS
2133 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
2134 Py_END_ALLOW_THREADS
2135
2136 return value;
2137}
2138
2139static PyObject *
2140posix_times(self, args)
2141 PyObject *self;
2142 PyObject *args;
2143{
2144 if (!PyArg_NoArgs(args))
2145 return NULL;
2146
2147 /* Currently Only Uptime is Provided -- Others Later */
2148 return Py_BuildValue("ddddd",
2149 (double)0 /* t.tms_utime / HZ */,
2150 (double)0 /* t.tms_stime / HZ */,
2151 (double)0 /* t.tms_cutime / HZ */,
2152 (double)0 /* t.tms_cstime / HZ */,
2153 (double)system_uptime() / 1000);
2154}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002155#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002156static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00002157posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002158 PyObject *self;
2159 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00002160{
2161 struct tms t;
2162 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00002163 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00002164 return NULL;
2165 errno = 0;
2166 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00002167 if (c == (clock_t) -1)
2168 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002169 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002170 (double)t.tms_utime / HZ,
2171 (double)t.tms_stime / HZ,
2172 (double)t.tms_cutime / HZ,
2173 (double)t.tms_cstime / HZ,
2174 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00002175}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002176#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002177#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002178
2179
Guido van Rossum87755a21996-09-07 00:59:43 +00002180#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002181#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00002182static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002183posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002184 PyObject *self;
2185 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002186{
2187 FILETIME create, exit, kernel, user;
2188 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00002189 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002190 return NULL;
2191 hProc = GetCurrentProcess();
2192 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00002193 return Py_BuildValue(
2194 "ddddd",
2195 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
2196 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
2197 (double)0,
2198 (double)0,
2199 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002200}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002201#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002202
2203#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00002204static char posix_times__doc__[] =
2205"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2206Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002207#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002208
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002209
Guido van Rossumb6775db1994-08-01 11:34:53 +00002210#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002211static char posix_setsid__doc__[] =
2212"setsid() -> None\n\
2213Call the system call setsid().";
2214
Barry Warsaw53699e91996-12-10 23:23:01 +00002215static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002216posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002217 PyObject *self;
2218 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002219{
Barry Warsaw53699e91996-12-10 23:23:01 +00002220 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002221 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002222 if (setsid() < 0)
2223 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002224 Py_INCREF(Py_None);
2225 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002226}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002227#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002228
Guido van Rossumb6775db1994-08-01 11:34:53 +00002229#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002230static char posix_setpgid__doc__[] =
2231"setpgid(pid, pgrp) -> None\n\
2232Call the system call setpgid().";
2233
Barry Warsaw53699e91996-12-10 23:23:01 +00002234static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002235posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002236 PyObject *self;
2237 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002238{
2239 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002240 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002241 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002242 if (setpgid(pid, pgrp) < 0)
2243 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002244 Py_INCREF(Py_None);
2245 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002246}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002247#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002248
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002249
Guido van Rossumb6775db1994-08-01 11:34:53 +00002250#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002251static char posix_tcgetpgrp__doc__[] =
2252"tcgetpgrp(fd) -> pgid\n\
2253Return the process group associated with the terminal given by a fd.";
2254
Barry Warsaw53699e91996-12-10 23:23:01 +00002255static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002256posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002257 PyObject *self;
2258 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002259{
2260 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002261 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002262 return NULL;
2263 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002264 if (pgid < 0)
2265 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002266 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002267}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002268#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002269
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002270
Guido van Rossumb6775db1994-08-01 11:34:53 +00002271#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002272static char posix_tcsetpgrp__doc__[] =
2273"tcsetpgrp(fd, pgid) -> None\n\
2274Set the process group associated with the terminal given by a fd.";
2275
Barry Warsaw53699e91996-12-10 23:23:01 +00002276static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002277posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002278 PyObject *self;
2279 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002280{
2281 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002282 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002283 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002284 if (tcsetpgrp(fd, pgid) < 0)
2285 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002286 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002287 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002288}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002289#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002290
Guido van Rossum687dd131993-05-17 08:34:16 +00002291/* Functions acting on file descriptors */
2292
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002293static char posix_open__doc__[] =
2294"open(filename, flag [, mode=0777]) -> fd\n\
2295Open a file (for low level IO).";
2296
Barry Warsaw53699e91996-12-10 23:23:01 +00002297static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002298posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002299 PyObject *self;
2300 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002301{
2302 char *file;
2303 int flag;
2304 int mode = 0777;
2305 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002306 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2307 return NULL;
2308
Barry Warsaw53699e91996-12-10 23:23:01 +00002309 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002310 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002311 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002312 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002313 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002314 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002315}
2316
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002317
2318static char posix_close__doc__[] =
2319"close(fd) -> None\n\
2320Close a file descriptor (for low level IO).";
2321
Barry Warsaw53699e91996-12-10 23:23:01 +00002322static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002323posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002324 PyObject *self;
2325 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002326{
2327 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002328 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002329 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002330 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002331 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002332 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002333 if (res < 0)
2334 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002335 Py_INCREF(Py_None);
2336 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002337}
2338
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002339
2340static char posix_dup__doc__[] =
2341"dup(fd) -> fd2\n\
2342Return a duplicate of a file descriptor.";
2343
Barry Warsaw53699e91996-12-10 23:23:01 +00002344static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002345posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002346 PyObject *self;
2347 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002348{
2349 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002350 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002351 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002352 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002353 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002354 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002355 if (fd < 0)
2356 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002357 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002358}
2359
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002360
2361static char posix_dup2__doc__[] =
2362"dup2(fd, fd2) -> None\n\
2363Duplicate file descriptor.";
2364
Barry Warsaw53699e91996-12-10 23:23:01 +00002365static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002366posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002367 PyObject *self;
2368 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002369{
2370 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002371 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002372 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002373 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002374 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002375 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002376 if (res < 0)
2377 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002378 Py_INCREF(Py_None);
2379 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002380}
2381
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002382
2383static char posix_lseek__doc__[] =
2384"lseek(fd, pos, how) -> newpos\n\
2385Set the current position of a file descriptor.";
2386
Barry Warsaw53699e91996-12-10 23:23:01 +00002387static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002388posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002389 PyObject *self;
2390 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002391{
2392 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002393 off_t pos, res;
2394 PyObject *posobj;
2395 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002396 return NULL;
2397#ifdef SEEK_SET
2398 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2399 switch (how) {
2400 case 0: how = SEEK_SET; break;
2401 case 1: how = SEEK_CUR; break;
2402 case 2: how = SEEK_END; break;
2403 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002404#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002405
2406#if !defined(HAVE_LARGEFILE_SUPPORT)
2407 pos = PyInt_AsLong(posobj);
2408#else
2409 pos = PyLong_Check(posobj) ?
2410 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2411#endif
2412 if (PyErr_Occurred())
2413 return NULL;
2414
Barry Warsaw53699e91996-12-10 23:23:01 +00002415 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002416 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002417 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002418 if (res < 0)
2419 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002420
2421#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002422 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002423#else
2424 return PyLong_FromLongLong(res);
2425#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002426}
2427
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002428
2429static char posix_read__doc__[] =
2430"read(fd, buffersize) -> string\n\
2431Read a file descriptor.";
2432
Barry Warsaw53699e91996-12-10 23:23:01 +00002433static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002434posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002435 PyObject *self;
2436 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002437{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002438 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002439 PyObject *buffer;
2440 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002441 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002442 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002443 if (buffer == NULL)
2444 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002445 Py_BEGIN_ALLOW_THREADS
2446 n = read(fd, PyString_AsString(buffer), size);
2447 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002448 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002449 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002450 return posix_error();
2451 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002452 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002453 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002454 return buffer;
2455}
2456
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002457
2458static char posix_write__doc__[] =
2459"write(fd, string) -> byteswritten\n\
2460Write a string to a file descriptor.";
2461
Barry Warsaw53699e91996-12-10 23:23:01 +00002462static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002463posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002464 PyObject *self;
2465 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002466{
2467 int fd, size;
2468 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002469 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002470 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002471 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002472 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002473 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002474 if (size < 0)
2475 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002476 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002477}
2478
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002479
2480static char posix_fstat__doc__[]=
2481"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2482Like stat(), but for an open file descriptor.";
2483
Barry Warsaw53699e91996-12-10 23:23:01 +00002484static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002485posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002486 PyObject *self;
2487 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002488{
2489 int fd;
2490 struct stat st;
2491 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002492 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002493 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002494 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002495 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002496 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002497 if (res != 0)
2498 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002499#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002500 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002501 (long)st.st_mode,
2502 (long)st.st_ino,
2503 (long)st.st_dev,
2504 (long)st.st_nlink,
2505 (long)st.st_uid,
2506 (long)st.st_gid,
2507 (long)st.st_size,
2508 (long)st.st_atime,
2509 (long)st.st_mtime,
2510 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002511#else
2512 return Py_BuildValue("(lLllllLlll)",
2513 (long)st.st_mode,
2514 (LONG_LONG)st.st_ino,
2515 (long)st.st_dev,
2516 (long)st.st_nlink,
2517 (long)st.st_uid,
2518 (long)st.st_gid,
2519 (LONG_LONG)st.st_size,
2520 (long)st.st_atime,
2521 (long)st.st_mtime,
2522 (long)st.st_ctime);
2523#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002524}
2525
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002526
2527static char posix_fdopen__doc__[] =
2528"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2529Return an open file object connected to a file descriptor.";
2530
Barry Warsaw53699e91996-12-10 23:23:01 +00002531static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002532posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002533 PyObject *self;
2534 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002535{
Barry Warsaw53699e91996-12-10 23:23:01 +00002536 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002537 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002538 char *mode = "r";
2539 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002540 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002541 PyObject *f;
2542 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002543 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002544
Barry Warsaw53699e91996-12-10 23:23:01 +00002545 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002546 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002547 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002548 if (fp == NULL)
2549 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002550 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002551 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002552 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002553 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002554}
2555
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002556
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002557#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002558static char posix_pipe__doc__[] =
2559"pipe() -> (read_end, write_end)\n\
2560Create a pipe.";
2561
Barry Warsaw53699e91996-12-10 23:23:01 +00002562static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002563posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002564 PyObject *self;
2565 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002566{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002567#if defined(PYOS_OS2)
2568 HFILE read, write;
2569 APIRET rc;
2570
2571 if (!PyArg_Parse(args, ""))
2572 return NULL;
2573
2574 Py_BEGIN_ALLOW_THREADS
2575 rc = DosCreatePipe( &read, &write, 4096);
2576 Py_END_ALLOW_THREADS
2577 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002578 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002579
2580 return Py_BuildValue("(ii)", read, write);
2581#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002582#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002583 int fds[2];
2584 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002585 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002586 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002587 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002588 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002589 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002590 if (res != 0)
2591 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002592 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002593#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002594 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002595 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002596 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002597 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002598 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002599 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002600 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002601 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002602 if (!ok)
2603 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002604 read_fd = _open_osfhandle((long)read, 0);
2605 write_fd = _open_osfhandle((long)write, 1);
2606 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002607#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002608#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002609}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002610#endif /* HAVE_PIPE */
2611
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002612
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002613#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002614static char posix_mkfifo__doc__[] =
2615"mkfifo(file, [, mode=0666]) -> None\n\
2616Create a FIFO (a POSIX named pipe).";
2617
Barry Warsaw53699e91996-12-10 23:23:01 +00002618static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002619posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002620 PyObject *self;
2621 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002622{
2623 char *file;
2624 int mode = 0666;
2625 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002626 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002627 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002628 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002629 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002630 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002631 if (res < 0)
2632 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002633 Py_INCREF(Py_None);
2634 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002635}
2636#endif
2637
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002638
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002639#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002640static char posix_ftruncate__doc__[] =
2641"ftruncate(fd, length) -> None\n\
2642Truncate a file to a specified length.";
2643
Barry Warsaw53699e91996-12-10 23:23:01 +00002644static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002645posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002646 PyObject *self; /* Not used */
2647 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002648{
2649 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002650 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002651 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002652 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002653
Guido van Rossum94f6f721999-01-06 18:42:14 +00002654 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2655 return NULL;
2656
2657#if !defined(HAVE_LARGEFILE_SUPPORT)
2658 length = PyInt_AsLong(lenobj);
2659#else
2660 length = PyLong_Check(lenobj) ?
2661 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2662#endif
2663 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002664 return NULL;
2665
Barry Warsaw53699e91996-12-10 23:23:01 +00002666 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002667 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002668 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002669 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002670 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002671 return NULL;
2672 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002673 Py_INCREF(Py_None);
2674 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002675}
2676#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002677
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002678#ifdef NeXT
2679#define HAVE_PUTENV
2680/* Steve Spicklemire got this putenv from NeXTAnswers */
2681static int
2682putenv(char *newval)
2683{
2684 extern char **environ;
2685
2686 static int firstTime = 1;
2687 char **ep;
2688 char *cp;
2689 int esiz;
2690 char *np;
2691
2692 if (!(np = strchr(newval, '=')))
2693 return 1;
2694 *np = '\0';
2695
2696 /* look it up */
2697 for (ep=environ ; *ep ; ep++)
2698 {
2699 /* this should always be true... */
2700 if (cp = strchr(*ep, '='))
2701 {
2702 *cp = '\0';
2703 if (!strcmp(*ep, newval))
2704 {
2705 /* got it! */
2706 *cp = '=';
2707 break;
2708 }
2709 *cp = '=';
2710 }
2711 else
2712 {
2713 *np = '=';
2714 return 1;
2715 }
2716 }
2717
2718 *np = '=';
2719 if (*ep)
2720 {
2721 /* the string was already there:
2722 just replace it with the new one */
2723 *ep = newval;
2724 return 0;
2725 }
2726
2727 /* expand environ by one */
2728 for (esiz=2, ep=environ ; *ep ; ep++)
2729 esiz++;
2730 if (firstTime)
2731 {
2732 char **epp;
2733 char **newenv;
2734 if (!(newenv = malloc(esiz * sizeof(char *))))
2735 return 1;
2736
2737 for (ep=environ, epp=newenv ; *ep ;)
2738 *epp++ = *ep++;
2739 *epp++ = newval;
2740 *epp = (char *) 0;
2741 environ = newenv;
2742 }
2743 else
2744 {
2745 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2746 return 1;
2747 environ[esiz - 2] = newval;
2748 environ[esiz - 1] = (char *) 0;
2749 firstTime = 0;
2750 }
2751
2752 return 0;
2753}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002754#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002755
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002756
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002757#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002758static char posix_putenv__doc__[] =
2759"putenv(key, value) -> None\n\
2760Change or add an environment variable.";
2761
Guido van Rossumbcc20741998-08-04 22:53:56 +00002762#ifdef __BEOS__
2763/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2764int putenv( const char *str );
2765#endif
2766
Barry Warsaw53699e91996-12-10 23:23:01 +00002767static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002768posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002769 PyObject *self;
2770 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002771{
2772 char *s1, *s2;
2773 char *new;
2774
Barry Warsaw53699e91996-12-10 23:23:01 +00002775 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002776 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002777
2778#if defined(PYOS_OS2)
2779 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2780 APIRET rc;
2781
2782 if (strlen(s2) == 0) /* If New Value is an Empty String */
2783 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2784
2785 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2786 if (rc != NO_ERROR)
2787 return os2_error(rc);
2788
2789 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2790 APIRET rc;
2791
2792 if (strlen(s2) == 0) /* If New Value is an Empty String */
2793 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2794
2795 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2796 if (rc != NO_ERROR)
2797 return os2_error(rc);
2798 } else {
2799#endif
2800
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002801 /* XXX This leaks memory -- not easy to fix :-( */
2802 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002803 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002804 (void) sprintf(new, "%s=%s", s1, s2);
2805 if (putenv(new)) {
2806 posix_error();
2807 return NULL;
2808 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002809
2810#if defined(PYOS_OS2)
2811 }
2812#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002813 Py_INCREF(Py_None);
2814 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002815}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002816#endif /* putenv */
2817
2818#ifdef HAVE_STRERROR
2819static char posix_strerror__doc__[] =
2820"strerror(code) -> string\n\
2821Translate an error code to a message string.";
2822
2823PyObject *
2824posix_strerror(self, args)
2825 PyObject *self;
2826 PyObject *args;
2827{
2828 int code;
2829 char *message;
2830 if (!PyArg_ParseTuple(args, "i", &code))
2831 return NULL;
2832 message = strerror(code);
2833 if (message == NULL) {
2834 PyErr_SetString(PyExc_ValueError,
2835 "strerror code out of range");
2836 return NULL;
2837 }
2838 return PyString_FromString(message);
2839}
2840#endif /* strerror */
2841
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002842
Guido van Rossumc9641791998-08-04 15:26:23 +00002843#ifdef HAVE_SYS_WAIT_H
2844
2845#ifdef WIFSTOPPED
2846static char posix_WIFSTOPPED__doc__[] =
2847"WIFSTOPPED(status) -> Boolean\n\
2848See Unix documentation.";
2849
2850static PyObject *
2851posix_WIFSTOPPED(self, args)
2852 PyObject *self;
2853 PyObject *args;
2854{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002855#ifdef UNION_WAIT
2856 union wait status;
2857#define status_i (status.w_status)
2858#else
2859 int status;
2860#define status_i status
2861#endif
2862 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002863
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002864 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002865 {
2866 return NULL;
2867 }
2868
2869 return Py_BuildValue("i", WIFSTOPPED(status));
2870}
2871#endif /* WIFSTOPPED */
2872
2873#ifdef WIFSIGNALED
2874static char posix_WIFSIGNALED__doc__[] =
2875"WIFSIGNALED(status) -> Boolean\n\
2876See Unix documentation.";
2877
2878static PyObject *
2879posix_WIFSIGNALED(self, args)
2880 PyObject *self;
2881 PyObject *args;
2882{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002883#ifdef UNION_WAIT
2884 union wait status;
2885#define status_i (status.w_status)
2886#else
2887 int status;
2888#define status_i status
2889#endif
2890 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002891
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002892 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002893 {
2894 return NULL;
2895 }
2896
2897 return Py_BuildValue("i", WIFSIGNALED(status));
2898}
2899#endif /* WIFSIGNALED */
2900
2901#ifdef WIFEXITED
2902static char posix_WIFEXITED__doc__[] =
2903"WIFEXITED(status) -> Boolean\n\
2904See Unix documentation.";
2905
2906static PyObject *
2907posix_WIFEXITED(self, args)
2908 PyObject *self;
2909 PyObject *args;
2910{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002911#ifdef UNION_WAIT
2912 union wait status;
2913#define status_i (status.w_status)
2914#else
2915 int status;
2916#define status_i status
2917#endif
2918 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002919
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002920 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002921 {
2922 return NULL;
2923 }
2924
2925 return Py_BuildValue("i", WIFEXITED(status));
2926}
2927#endif /* WIFEXITED */
2928
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002929#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00002930static char posix_WEXITSTATUS__doc__[] =
2931"WEXITSTATUS(status) -> integer\n\
2932See Unix documentation.";
2933
2934static PyObject *
2935posix_WEXITSTATUS(self, args)
2936 PyObject *self;
2937 PyObject *args;
2938{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002939#ifdef UNION_WAIT
2940 union wait status;
2941#define status_i (status.w_status)
2942#else
2943 int status;
2944#define status_i status
2945#endif
2946 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002947
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002948 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002949 {
2950 return NULL;
2951 }
2952
2953 return Py_BuildValue("i", WEXITSTATUS(status));
2954}
2955#endif /* WEXITSTATUS */
2956
2957#ifdef WTERMSIG
2958static char posix_WTERMSIG__doc__[] =
2959"WTERMSIG(status) -> integer\n\
2960See Unix documentation.";
2961
2962static PyObject *
2963posix_WTERMSIG(self, args)
2964 PyObject *self;
2965 PyObject *args;
2966{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002967#ifdef UNION_WAIT
2968 union wait status;
2969#define status_i (status.w_status)
2970#else
2971 int status;
2972#define status_i status
2973#endif
2974 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002975
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002976 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002977 {
2978 return NULL;
2979 }
2980
2981 return Py_BuildValue("i", WTERMSIG(status));
2982}
2983#endif /* WTERMSIG */
2984
2985#ifdef WSTOPSIG
2986static char posix_WSTOPSIG__doc__[] =
2987"WSTOPSIG(status) -> integer\n\
2988See Unix documentation.";
2989
2990static PyObject *
2991posix_WSTOPSIG(self, args)
2992 PyObject *self;
2993 PyObject *args;
2994{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002995#ifdef UNION_WAIT
2996 union wait status;
2997#define status_i (status.w_status)
2998#else
2999 int status;
3000#define status_i status
3001#endif
3002 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003003
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003004 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003005 {
3006 return NULL;
3007 }
3008
3009 return Py_BuildValue("i", WSTOPSIG(status));
3010}
3011#endif /* WSTOPSIG */
3012
3013#endif /* HAVE_SYS_WAIT_H */
3014
3015
Guido van Rossum94f6f721999-01-06 18:42:14 +00003016#if defined(HAVE_FSTATVFS)
3017#include <sys/statvfs.h>
3018
3019static char posix_fstatvfs__doc__[] =
3020"fstatvfs(fd) -> \
3021(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
3022Perform an fstatvfs system call on the given fd.";
3023
3024static PyObject *
3025posix_fstatvfs(self, args)
3026 PyObject *self;
3027 PyObject *args;
3028{
3029 int fd, res;
3030 struct statvfs st;
3031 if (!PyArg_ParseTuple(args, "i", &fd))
3032 return NULL;
3033 Py_BEGIN_ALLOW_THREADS
3034 res = fstatvfs(fd, &st);
3035 Py_END_ALLOW_THREADS
3036 if (res != 0)
3037 return posix_error();
3038#if !defined(HAVE_LARGEFILE_SUPPORT)
3039 return Py_BuildValue("(lllllllllll)",
3040 (long) st.f_bsize,
3041 (long) st.f_frsize,
3042 (long) st.f_blocks,
3043 (long) st.f_bfree,
3044 (long) st.f_bavail,
3045 (long) st.f_files,
3046 (long) st.f_ffree,
3047 (long) st.f_favail,
3048 (long) st.f_fsid,
3049 (long) st.f_flag,
3050 (long) st.f_namemax);
3051#else
3052 return Py_BuildValue("(llLLLLLLlll)",
3053 (long) st.f_bsize,
3054 (long) st.f_frsize,
3055 (LONG_LONG) st.f_blocks,
3056 (LONG_LONG) st.f_bfree,
3057 (LONG_LONG) st.f_bavail,
3058 (LONG_LONG) st.f_files,
3059 (LONG_LONG) st.f_ffree,
3060 (LONG_LONG) st.f_favail,
3061 (long) st.f_fsid,
3062 (long) st.f_flag,
3063 (long) st.f_namemax);
3064#endif
3065}
3066#endif /* HAVE_FSTATVFS */
3067
3068
3069#if defined(HAVE_STATVFS)
3070#include <sys/statvfs.h>
3071
3072static char posix_statvfs__doc__[] =
3073"statvfs(path) -> \
3074(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
3075Perform a statvfs system call on the given path.";
3076
3077static PyObject *
3078posix_statvfs(self, args)
3079 PyObject *self;
3080 PyObject *args;
3081{
3082 char *path;
3083 int res;
3084 struct statvfs st;
3085 if (!PyArg_ParseTuple(args, "s", &path))
3086 return NULL;
3087 Py_BEGIN_ALLOW_THREADS
3088 res = statvfs(path, &st);
3089 Py_END_ALLOW_THREADS
3090 if (res != 0)
3091 return posix_error_with_filename(path);
3092#if !defined(HAVE_LARGEFILE_SUPPORT)
3093 return Py_BuildValue("(lllllllllll)",
3094 (long) st.f_bsize,
3095 (long) st.f_frsize,
3096 (long) st.f_blocks,
3097 (long) st.f_bfree,
3098 (long) st.f_bavail,
3099 (long) st.f_files,
3100 (long) st.f_ffree,
3101 (long) st.f_favail,
3102 (long) st.f_fsid,
3103 (long) st.f_flag,
3104 (long) st.f_namemax);
3105#else /* HAVE_LARGEFILE_SUPPORT */
3106 return Py_BuildValue("(llLLLLLLlll)",
3107 (long) st.f_bsize,
3108 (long) st.f_frsize,
3109 (LONG_LONG) st.f_blocks,
3110 (LONG_LONG) st.f_bfree,
3111 (LONG_LONG) st.f_bavail,
3112 (LONG_LONG) st.f_files,
3113 (LONG_LONG) st.f_ffree,
3114 (LONG_LONG) st.f_favail,
3115 (long) st.f_fsid,
3116 (long) st.f_flag,
3117 (long) st.f_namemax);
3118#endif
3119}
3120#endif /* HAVE_STATVFS */
3121
3122
Barry Warsaw53699e91996-12-10 23:23:01 +00003123static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00003124 {"access", posix_access, 0, posix_access__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003125#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +00003126 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003127#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003128 {"chdir", posix_chdir, 0, posix_chdir__doc__},
3129 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003130#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003131 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003132#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00003133#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003134 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00003135#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00003136#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003137 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003138#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003139 {"listdir", posix_listdir, 0, posix_listdir__doc__},
3140 {"lstat", posix_lstat, 0, posix_lstat__doc__},
3141 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003142#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003143 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003144#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003145#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003146 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003147#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003148 {"rename", posix_rename, 0, posix_rename__doc__},
3149 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
3150 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003151#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003152 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003153#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003154#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003155 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003156#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003157 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003158#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003159 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003160#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003161 {"unlink", posix_unlink, 0, posix_unlink__doc__},
3162 {"remove", posix_unlink, 0, posix_remove__doc__},
3163 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003164#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003165 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003166#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003167 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003168#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003169 {"execv", posix_execv, 0, posix_execv__doc__},
3170 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003171#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00003172#ifdef HAVE_SPAWNV
3173 {"spawnv", posix_spawnv, 0, posix_spawnv__doc__},
3174 {"spawnve", posix_spawnve, 0, posix_spawnve__doc__},
3175#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003176#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003177 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003178#endif /* HAVE_FORK */
3179#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003180 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003181#endif /* HAVE_GETEGID */
3182#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003183 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003184#endif /* HAVE_GETEUID */
3185#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003186 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003187#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003188 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003189#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003190 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003191#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003192#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003193 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003194#endif /* HAVE_GETPPID */
3195#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003196 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003197#endif /* HAVE_GETUID */
3198#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003199 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003200#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00003201#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003202 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00003203#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003204#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003205 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003206#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003207#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003208 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003209#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003210#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003211 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003212#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003213#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003214 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003215#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003216#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003217 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003218#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003219#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003220 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003221#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003222#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003223 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003224#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003225#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003226 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003227#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003228#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003229 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003230#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003231#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003232 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003233#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003234 {"open", posix_open, 1, posix_open__doc__},
3235 {"close", posix_close, 0, posix_close__doc__},
3236 {"dup", posix_dup, 0, posix_dup__doc__},
3237 {"dup2", posix_dup2, 0, posix_dup2__doc__},
3238 {"lseek", posix_lseek, 0, posix_lseek__doc__},
3239 {"read", posix_read, 0, posix_read__doc__},
3240 {"write", posix_write, 0, posix_write__doc__},
3241 {"fstat", posix_fstat, 0, posix_fstat__doc__},
3242 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003243#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003244 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003245#endif
3246#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003247 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003248#endif
3249#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003250 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003251#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003252#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003253 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003254#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00003255#ifdef HAVE_STRERROR
3256 {"strerror", posix_strerror, 1, posix_strerror__doc__},
3257#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00003258#ifdef HAVE_FSYNC
3259 {"fsync", posix_fsync, 0, posix_fsync__doc__},
3260#endif
3261#ifdef HAVE_FDATASYNC
3262 {"fdatasync", posix_fdatasync, 0, posix_fdatasync__doc__},
3263#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00003264#ifdef HAVE_SYS_WAIT_H
3265#ifdef WIFSTOPPED
3266 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
3267#endif /* WIFSTOPPED */
3268#ifdef WIFSIGNALED
3269 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
3270#endif /* WIFSIGNALED */
3271#ifdef WIFEXITED
3272 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
3273#endif /* WIFEXITED */
3274#ifdef WEXITSTATUS
3275 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
3276#endif /* WEXITSTATUS */
3277#ifdef WTERMSIG
3278 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
3279#endif /* WTERMSIG */
3280#ifdef WSTOPSIG
3281 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
3282#endif /* WSTOPSIG */
3283#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003284#ifdef HAVE_FSTATVFS
3285 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
3286#endif
3287#ifdef HAVE_STATVFS
3288 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
3289#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003290 {NULL, NULL} /* Sentinel */
3291};
3292
3293
Barry Warsaw4a342091996-12-19 23:50:02 +00003294static int
3295ins(d, symbol, value)
3296 PyObject* d;
3297 char* symbol;
3298 long value;
3299{
3300 PyObject* v = PyInt_FromLong(value);
3301 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
3302 return -1; /* triggers fatal error */
3303
3304 Py_DECREF(v);
3305 return 0;
3306}
3307
Guido van Rossumd48f2521997-12-05 22:19:34 +00003308#if defined(PYOS_OS2)
3309/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
3310static int insertvalues(PyObject *d)
3311{
3312 APIRET rc;
3313 ULONG values[QSV_MAX+1];
3314 PyObject *v;
3315 char *ver, tmp[10];
3316
3317 Py_BEGIN_ALLOW_THREADS
3318 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
3319 Py_END_ALLOW_THREADS
3320
3321 if (rc != NO_ERROR) {
3322 os2_error(rc);
3323 return -1;
3324 }
3325
3326 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3327 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3328 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3329 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3330 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3331 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3332 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3333
3334 switch (values[QSV_VERSION_MINOR]) {
3335 case 0: ver = "2.00"; break;
3336 case 10: ver = "2.10"; break;
3337 case 11: ver = "2.11"; break;
3338 case 30: ver = "3.00"; break;
3339 case 40: ver = "4.00"; break;
3340 case 50: ver = "5.00"; break;
3341 default:
3342 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3343 values[QSV_VERSION_MINOR]);
3344 ver = &tmp[0];
3345 }
3346
3347 /* Add Indicator of the Version of the Operating System */
3348 v = PyString_FromString(ver);
3349 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3350 return -1;
3351 Py_DECREF(v);
3352
3353 /* Add Indicator of Which Drive was Used to Boot the System */
3354 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3355 tmp[1] = ':';
3356 tmp[2] = '\0';
3357
3358 v = PyString_FromString(tmp);
3359 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3360 return -1;
3361 Py_DECREF(v);
3362
3363 return 0;
3364}
3365#endif
3366
Barry Warsaw4a342091996-12-19 23:50:02 +00003367static int
3368all_ins(d)
3369 PyObject* d;
3370{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003371#ifdef F_OK
3372 if (ins(d, "F_OK", (long)F_OK)) return -1;
3373#endif
3374#ifdef R_OK
3375 if (ins(d, "R_OK", (long)R_OK)) return -1;
3376#endif
3377#ifdef W_OK
3378 if (ins(d, "W_OK", (long)W_OK)) return -1;
3379#endif
3380#ifdef X_OK
3381 if (ins(d, "X_OK", (long)X_OK)) return -1;
3382#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003383#ifdef WNOHANG
3384 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3385#endif
3386#ifdef O_RDONLY
3387 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3388#endif
3389#ifdef O_WRONLY
3390 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3391#endif
3392#ifdef O_RDWR
3393 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3394#endif
3395#ifdef O_NDELAY
3396 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3397#endif
3398#ifdef O_NONBLOCK
3399 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3400#endif
3401#ifdef O_APPEND
3402 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3403#endif
3404#ifdef O_DSYNC
3405 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3406#endif
3407#ifdef O_RSYNC
3408 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3409#endif
3410#ifdef O_SYNC
3411 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3412#endif
3413#ifdef O_NOCTTY
3414 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3415#endif
3416#ifdef O_CREAT
3417 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3418#endif
3419#ifdef O_EXCL
3420 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3421#endif
3422#ifdef O_TRUNC
3423 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3424#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003425#ifdef O_BINARY
3426 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3427#endif
3428#ifdef O_TEXT
3429 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3430#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003431
3432#if defined(PYOS_OS2)
3433 if (insertvalues(d)) return -1;
3434#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003435 return 0;
3436}
3437
3438
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003439#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003440#define INITFUNC initnt
3441#define MODNAME "nt"
3442#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003443#if defined(PYOS_OS2)
3444#define INITFUNC initos2
3445#define MODNAME "os2"
3446#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003447#define INITFUNC initposix
3448#define MODNAME "posix"
3449#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003450#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003451
Guido van Rossum3886bb61998-12-04 18:50:17 +00003452DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003453INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003454{
Barry Warsaw53699e91996-12-10 23:23:01 +00003455 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003456
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003457 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003458 posix_methods,
3459 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003460 (PyObject *)NULL,
3461 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003462 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003463
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003464 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003465 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003466 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003467 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003468 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003469
Barry Warsaw4a342091996-12-19 23:50:02 +00003470 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003471 return;
3472
Barry Warsawd58d7641998-07-23 16:14:40 +00003473 Py_INCREF(PyExc_OSError);
3474 PosixError = PyExc_OSError;
3475 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003476}