blob: 821a3cd96d674287c75ecf15392a9aebc214ea72 [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__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001416"spawnv(mode, path, args)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001417Execute an executable path with arguments, replacing current process.\n\
1418\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001419 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001420 path: path of executable file\n\
1421 args: tuple or list of strings";
1422
1423static PyObject *
1424posix_spawnv(self, args)
1425 PyObject *self;
1426 PyObject *args;
1427{
1428 char *path;
1429 PyObject *argv;
1430 char **argvlist;
1431 int mode, i, argc;
1432 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1433
1434 /* spawnv has three arguments: (mode, path, argv), where
1435 argv is a list or tuple of strings. */
1436
1437 if (!PyArg_Parse(args, "(isO)", &mode, &path, &argv))
1438 return NULL;
1439 if (PyList_Check(argv)) {
1440 argc = PyList_Size(argv);
1441 getitem = PyList_GetItem;
1442 }
1443 else if (PyTuple_Check(argv)) {
1444 argc = PyTuple_Size(argv);
1445 getitem = PyTuple_GetItem;
1446 }
1447 else {
1448 badarg:
1449 PyErr_BadArgument();
1450 return NULL;
1451 }
1452
1453 argvlist = PyMem_NEW(char *, argc+1);
1454 if (argvlist == NULL)
1455 return NULL;
1456 for (i = 0; i < argc; i++) {
1457 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1458 PyMem_DEL(argvlist);
1459 goto badarg;
1460 }
1461 }
1462 argvlist[argc] = NULL;
1463
Guido van Rossum246bc171999-02-01 23:54:31 +00001464 if (mode == _OLD_P_OVERLAY)
1465 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001466 i = _spawnv(mode, path, argvlist);
1467
1468 PyMem_DEL(argvlist);
1469
1470 if (i == -1)
1471 return posix_error();
1472 else
1473 return Py_BuildValue("i", i);
1474}
1475
1476
1477static char posix_spawnve__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001478"spawnve(mode, path, args, env)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001479Execute a path with arguments and environment, replacing current process.\n\
1480\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001481 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001482 path: path of executable file\n\
1483 args: tuple or list of arguments\n\
1484 env: dictonary of strings mapping to strings";
1485
1486static PyObject *
1487posix_spawnve(self, args)
1488 PyObject *self;
1489 PyObject *args;
1490{
1491 char *path;
1492 PyObject *argv, *env;
1493 char **argvlist;
1494 char **envlist;
1495 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1496 int mode, i, pos, argc, envc;
1497 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1498
1499 /* spawnve has four arguments: (mode, path, argv, env), where
1500 argv is a list or tuple of strings and env is a dictionary
1501 like posix.environ. */
1502
1503 if (!PyArg_Parse(args, "(isOO)", &mode, &path, &argv, &env))
1504 return NULL;
1505 if (PyList_Check(argv)) {
1506 argc = PyList_Size(argv);
1507 getitem = PyList_GetItem;
1508 }
1509 else if (PyTuple_Check(argv)) {
1510 argc = PyTuple_Size(argv);
1511 getitem = PyTuple_GetItem;
1512 }
1513 else {
1514 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1515 return NULL;
1516 }
1517 if (!PyMapping_Check(env)) {
1518 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1519 return NULL;
1520 }
1521
1522 argvlist = PyMem_NEW(char *, argc+1);
1523 if (argvlist == NULL) {
1524 PyErr_NoMemory();
1525 return NULL;
1526 }
1527 for (i = 0; i < argc; i++) {
1528 if (!PyArg_Parse((*getitem)(argv, i),
1529 "s;argv must be list of strings",
1530 &argvlist[i]))
1531 {
1532 goto fail_1;
1533 }
1534 }
1535 argvlist[argc] = NULL;
1536
1537 i = PyMapping_Length(env);
1538 envlist = PyMem_NEW(char *, i + 1);
1539 if (envlist == NULL) {
1540 PyErr_NoMemory();
1541 goto fail_1;
1542 }
1543 envc = 0;
1544 keys = PyMapping_Keys(env);
1545 vals = PyMapping_Values(env);
1546 if (!keys || !vals)
1547 goto fail_2;
1548
1549 for (pos = 0; pos < i; pos++) {
1550 char *p, *k, *v;
1551
1552 key = PyList_GetItem(keys, pos);
1553 val = PyList_GetItem(vals, pos);
1554 if (!key || !val)
1555 goto fail_2;
1556
1557 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1558 !PyArg_Parse(val, "s;non-string value in env", &v))
1559 {
1560 goto fail_2;
1561 }
1562 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1563 if (p == NULL) {
1564 PyErr_NoMemory();
1565 goto fail_2;
1566 }
1567 sprintf(p, "%s=%s", k, v);
1568 envlist[envc++] = p;
1569 }
1570 envlist[envc] = 0;
1571
Guido van Rossum246bc171999-02-01 23:54:31 +00001572 if (mode == _OLD_P_OVERLAY)
1573 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001574 i = _spawnve(mode, path, argvlist, envlist);
1575 if (i == -1)
1576 (void) posix_error();
1577 else
1578 res = Py_BuildValue("i", i);
1579
1580 fail_2:
1581 while (--envc >= 0)
1582 PyMem_DEL(envlist[envc]);
1583 PyMem_DEL(envlist);
1584 fail_1:
1585 PyMem_DEL(argvlist);
1586 Py_XDECREF(vals);
1587 Py_XDECREF(keys);
1588 return res;
1589}
1590#endif /* HAVE_SPAWNV */
1591
1592
Guido van Rossumad0ee831995-03-01 10:34:45 +00001593#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001594static char posix_fork__doc__[] =
1595"fork() -> pid\n\
1596Fork a child process.\n\
1597\n\
1598Return 0 to child process and PID of child to parent process.";
1599
Barry Warsaw53699e91996-12-10 23:23:01 +00001600static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001601posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001602 PyObject *self;
1603 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001604{
1605 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001606 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001607 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001608 pid = fork();
1609 if (pid == -1)
1610 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001611 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001612 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001613}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001614#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001615
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001616
Guido van Rossumad0ee831995-03-01 10:34:45 +00001617#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001618static char posix_getegid__doc__[] =
1619"getegid() -> egid\n\
1620Return the current process's effective group id.";
1621
Barry Warsaw53699e91996-12-10 23:23:01 +00001622static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001623posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001624 PyObject *self;
1625 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001626{
Barry Warsaw53699e91996-12-10 23:23:01 +00001627 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001628 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001629 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001630}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001631#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001633
Guido van Rossumad0ee831995-03-01 10:34:45 +00001634#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001635static char posix_geteuid__doc__[] =
1636"geteuid() -> euid\n\
1637Return the current process's effective user id.";
1638
Barry Warsaw53699e91996-12-10 23:23:01 +00001639static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001640posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001641 PyObject *self;
1642 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001643{
Barry Warsaw53699e91996-12-10 23:23:01 +00001644 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001645 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001646 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001647}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001648#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001649
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001650
Guido van Rossumad0ee831995-03-01 10:34:45 +00001651#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001652static char posix_getgid__doc__[] =
1653"getgid() -> gid\n\
1654Return the current process's group id.";
1655
Barry Warsaw53699e91996-12-10 23:23:01 +00001656static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001657posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001658 PyObject *self;
1659 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001660{
Barry Warsaw53699e91996-12-10 23:23:01 +00001661 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001662 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001663 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001664}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001665#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001666
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001667
1668static char posix_getpid__doc__[] =
1669"getpid() -> pid\n\
1670Return the current process id";
1671
Barry Warsaw53699e91996-12-10 23:23:01 +00001672static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001673posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001674 PyObject *self;
1675 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001676{
Barry Warsaw53699e91996-12-10 23:23:01 +00001677 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001678 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001679 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001680}
1681
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001682
Guido van Rossumb6775db1994-08-01 11:34:53 +00001683#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001684static char posix_getpgrp__doc__[] =
1685"getpgrp() -> pgrp\n\
1686Return the current process group id.";
1687
Barry Warsaw53699e91996-12-10 23:23:01 +00001688static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001689posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001690 PyObject *self;
1691 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001692{
Barry Warsaw53699e91996-12-10 23:23:01 +00001693 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001694 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001695#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001696 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001697#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001698 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001699#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001700}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001701#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001702
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001703
Guido van Rossumb6775db1994-08-01 11:34:53 +00001704#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001705static char posix_setpgrp__doc__[] =
1706"setpgrp() -> None\n\
1707Make this process a session leader.";
1708
Barry Warsaw53699e91996-12-10 23:23:01 +00001709static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001710posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001711 PyObject *self;
1712 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001713{
Barry Warsaw53699e91996-12-10 23:23:01 +00001714 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001715 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001716#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001717 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001718#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001719 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001720#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001721 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001722 Py_INCREF(Py_None);
1723 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001724}
1725
Guido van Rossumb6775db1994-08-01 11:34:53 +00001726#endif /* HAVE_SETPGRP */
1727
Guido van Rossumad0ee831995-03-01 10:34:45 +00001728#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001729static char posix_getppid__doc__[] =
1730"getppid() -> ppid\n\
1731Return the parent's process id.";
1732
Barry Warsaw53699e91996-12-10 23:23:01 +00001733static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001734posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001735 PyObject *self;
1736 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001737{
Barry Warsaw53699e91996-12-10 23:23:01 +00001738 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001739 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001740 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001741}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001742#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001743
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001744
Guido van Rossumad0ee831995-03-01 10:34:45 +00001745#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001746static char posix_getuid__doc__[] =
1747"getuid() -> uid\n\
1748Return the current process's user id.";
1749
Barry Warsaw53699e91996-12-10 23:23:01 +00001750static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001751posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001752 PyObject *self;
1753 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001754{
Barry Warsaw53699e91996-12-10 23:23:01 +00001755 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001756 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001757 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001758}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001759#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001760
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001761
Guido van Rossumad0ee831995-03-01 10:34:45 +00001762#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001763static char posix_kill__doc__[] =
1764"kill(pid, sig) -> None\n\
1765Kill a process with a signal.";
1766
Barry Warsaw53699e91996-12-10 23:23:01 +00001767static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001768posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001769 PyObject *self;
1770 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001771{
1772 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001773 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001774 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001775#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001776 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1777 APIRET rc;
1778 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001779 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001780
1781 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1782 APIRET rc;
1783 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001784 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001785
1786 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001787 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001788#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001789 if (kill(pid, sig) == -1)
1790 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001791#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001792 Py_INCREF(Py_None);
1793 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001794}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001795#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001796
Guido van Rossumc0125471996-06-28 18:55:32 +00001797#ifdef HAVE_PLOCK
1798
1799#ifdef HAVE_SYS_LOCK_H
1800#include <sys/lock.h>
1801#endif
1802
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001803static char posix_plock__doc__[] =
1804"plock(op) -> None\n\
1805Lock program segments into memory.";
1806
Barry Warsaw53699e91996-12-10 23:23:01 +00001807static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001808posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001809 PyObject *self;
1810 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001811{
1812 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001813 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001814 return NULL;
1815 if (plock(op) == -1)
1816 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001817 Py_INCREF(Py_None);
1818 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001819}
1820#endif
1821
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001822
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001823#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001824static char posix_popen__doc__[] =
1825"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1826Open a pipe to/from a command returning a file object.";
1827
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001828#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001829static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001830async_system(const char *command)
1831{
1832 char *p, errormsg[256], args[1024];
1833 RESULTCODES rcodes;
1834 APIRET rc;
1835 char *shell = getenv("COMSPEC");
1836 if (!shell)
1837 shell = "cmd";
1838
1839 strcpy(args, shell);
1840 p = &args[ strlen(args)+1 ];
1841 strcpy(p, "/c ");
1842 strcat(p, command);
1843 p += strlen(p) + 1;
1844 *p = '\0';
1845
1846 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001847 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001848 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001849 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001850 &rcodes, shell);
1851 return rc;
1852}
1853
Guido van Rossumd48f2521997-12-05 22:19:34 +00001854static FILE *
1855popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001856{
1857 HFILE rhan, whan;
1858 FILE *retfd = NULL;
1859 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1860
Guido van Rossumd48f2521997-12-05 22:19:34 +00001861 if (rc != NO_ERROR) {
1862 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001863 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001864 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001865
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001866 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1867 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001868
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001869 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1870 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001871
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001872 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1873 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001874
1875 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001876 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001877 }
1878
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001879 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1880 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001881
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001882 close(oldfd); /* And Close Saved STDOUT Handle */
1883 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001884
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001885 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1886 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001887
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001888 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1889 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001890
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001891 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1892 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001893
1894 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001895 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001896 }
1897
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001898 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1899 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001900
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001901 close(oldfd); /* And Close Saved STDIN Handle */
1902 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001903
Guido van Rossumd48f2521997-12-05 22:19:34 +00001904 } else {
1905 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001906 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001907 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001908}
1909
1910static PyObject *
1911posix_popen(self, args)
1912 PyObject *self;
1913 PyObject *args;
1914{
1915 char *name;
1916 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001917 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001918 FILE *fp;
1919 PyObject *f;
1920 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1921 return NULL;
1922 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001923 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001924 Py_END_ALLOW_THREADS
1925 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001926 return os2_error(err);
1927
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001928 f = PyFile_FromFile(fp, name, mode, fclose);
1929 if (f != NULL)
1930 PyFile_SetBufSize(f, bufsize);
1931 return f;
1932}
1933
1934#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001935static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001936posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001937 PyObject *self;
1938 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001939{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001940 char *name;
1941 char *mode = "r";
1942 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001943 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001944 PyObject *f;
1945 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001946 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001947 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001948 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001949 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001950 if (fp == NULL)
1951 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001952 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001953 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001954 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001955 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001956}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001957#endif
1958
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001959#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001960
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001961
Guido van Rossumb6775db1994-08-01 11:34:53 +00001962#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001963static char posix_setuid__doc__[] =
1964"setuid(uid) -> None\n\
1965Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001966static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001967posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001968 PyObject *self;
1969 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001970{
1971 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001972 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001973 return NULL;
1974 if (setuid(uid) < 0)
1975 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001976 Py_INCREF(Py_None);
1977 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001978}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001979#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001980
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001981
Guido van Rossumb6775db1994-08-01 11:34:53 +00001982#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001983static char posix_setgid__doc__[] =
1984"setgid(gid) -> None\n\
1985Set the current process's group id.";
1986
Barry Warsaw53699e91996-12-10 23:23:01 +00001987static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001988posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001989 PyObject *self;
1990 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001991{
1992 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001993 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001994 return NULL;
1995 if (setgid(gid) < 0)
1996 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001997 Py_INCREF(Py_None);
1998 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001999}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002000#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002001
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002002
Guido van Rossumb6775db1994-08-01 11:34:53 +00002003#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002004static char posix_waitpid__doc__[] =
2005"waitpid(pid, options) -> (pid, status)\n\
2006Wait for completion of a give child process.";
2007
Barry Warsaw53699e91996-12-10 23:23:01 +00002008static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002009posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002010 PyObject *self;
2011 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002012{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002013 int pid, options;
2014#ifdef UNION_WAIT
2015 union wait status;
2016#define status_i (status.w_status)
2017#else
2018 int status;
2019#define status_i status
2020#endif
2021 status_i = 0;
2022
Barry Warsaw53699e91996-12-10 23:23:01 +00002023 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002024 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002025 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002026#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002027 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002028#else
2029 pid = waitpid(pid, &status, options);
2030#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002031 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002032 if (pid == -1)
2033 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002034 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002035 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002036}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002037#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002038
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002039
Guido van Rossumad0ee831995-03-01 10:34:45 +00002040#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002041static char posix_wait__doc__[] =
2042"wait() -> (pid, status)\n\
2043Wait for completion of a child process.";
2044
Barry Warsaw53699e91996-12-10 23:23:01 +00002045static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002046posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002047 PyObject *self;
2048 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00002049{
2050 int pid, sts;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002051#ifdef UNION_WAIT
2052 union wait status;
2053#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002054#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002055 int status;
2056#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002057#endif
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002058 status_i = 0;
2059 Py_BEGIN_ALLOW_THREADS
2060 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00002061 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00002062 if (pid == -1)
2063 return posix_error();
2064 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002065 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002066}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002067#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002068
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002069
2070static char posix_lstat__doc__[] =
2071"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
2072Like stat(path), but do not follow symbolic links.";
2073
Barry Warsaw53699e91996-12-10 23:23:01 +00002074static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002075posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002076 PyObject *self;
2077 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002078{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002079#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002080 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002081#else /* !HAVE_LSTAT */
2082 return posix_do_stat(self, args, stat);
2083#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002084}
2085
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002086
Guido van Rossumb6775db1994-08-01 11:34:53 +00002087#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002088static char posix_readlink__doc__[] =
2089"readlink(path) -> path\n\
2090Return a string representing the path to which the symbolic link points.";
2091
Barry Warsaw53699e91996-12-10 23:23:01 +00002092static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002093posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002094 PyObject *self;
2095 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002096{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002097 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002098 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002099 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002100 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002101 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002102 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00002103 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00002104 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002105 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002106 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002107 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002108}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002109#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002110
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002111
Guido van Rossumb6775db1994-08-01 11:34:53 +00002112#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002113static char posix_symlink__doc__[] =
2114"symlink(src, dst) -> None\n\
2115Create a symbolic link.";
2116
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002117static PyObject *
2118posix_symlink(self, args)
2119 PyObject *self;
2120 PyObject *args;
2121{
2122 return posix_2str(args, symlink);
2123}
2124#endif /* HAVE_SYMLINK */
2125
2126
2127#ifdef HAVE_TIMES
2128#ifndef HZ
2129#define HZ 60 /* Universal constant :-) */
2130#endif /* HZ */
2131
Guido van Rossumd48f2521997-12-05 22:19:34 +00002132#if defined(PYCC_VACPP) && defined(PYOS_OS2)
2133static long
2134system_uptime()
2135{
2136 ULONG value = 0;
2137
2138 Py_BEGIN_ALLOW_THREADS
2139 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
2140 Py_END_ALLOW_THREADS
2141
2142 return value;
2143}
2144
2145static PyObject *
2146posix_times(self, args)
2147 PyObject *self;
2148 PyObject *args;
2149{
2150 if (!PyArg_NoArgs(args))
2151 return NULL;
2152
2153 /* Currently Only Uptime is Provided -- Others Later */
2154 return Py_BuildValue("ddddd",
2155 (double)0 /* t.tms_utime / HZ */,
2156 (double)0 /* t.tms_stime / HZ */,
2157 (double)0 /* t.tms_cutime / HZ */,
2158 (double)0 /* t.tms_cstime / HZ */,
2159 (double)system_uptime() / 1000);
2160}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002161#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002162static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00002163posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002164 PyObject *self;
2165 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00002166{
2167 struct tms t;
2168 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00002169 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00002170 return NULL;
2171 errno = 0;
2172 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00002173 if (c == (clock_t) -1)
2174 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002175 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002176 (double)t.tms_utime / HZ,
2177 (double)t.tms_stime / HZ,
2178 (double)t.tms_cutime / HZ,
2179 (double)t.tms_cstime / HZ,
2180 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00002181}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002182#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002183#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002184
2185
Guido van Rossum87755a21996-09-07 00:59:43 +00002186#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002187#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00002188static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002189posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002190 PyObject *self;
2191 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002192{
2193 FILETIME create, exit, kernel, user;
2194 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00002195 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002196 return NULL;
2197 hProc = GetCurrentProcess();
2198 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00002199 return Py_BuildValue(
2200 "ddddd",
2201 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
2202 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
2203 (double)0,
2204 (double)0,
2205 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002206}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002207#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002208
2209#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00002210static char posix_times__doc__[] =
2211"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2212Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002213#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002214
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002215
Guido van Rossumb6775db1994-08-01 11:34:53 +00002216#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002217static char posix_setsid__doc__[] =
2218"setsid() -> None\n\
2219Call the system call setsid().";
2220
Barry Warsaw53699e91996-12-10 23:23:01 +00002221static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002222posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002223 PyObject *self;
2224 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002225{
Barry Warsaw53699e91996-12-10 23:23:01 +00002226 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002227 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002228 if (setsid() < 0)
2229 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002230 Py_INCREF(Py_None);
2231 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002232}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002233#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002234
Guido van Rossumb6775db1994-08-01 11:34:53 +00002235#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002236static char posix_setpgid__doc__[] =
2237"setpgid(pid, pgrp) -> None\n\
2238Call the system call setpgid().";
2239
Barry Warsaw53699e91996-12-10 23:23:01 +00002240static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002241posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002242 PyObject *self;
2243 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002244{
2245 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002246 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002247 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002248 if (setpgid(pid, pgrp) < 0)
2249 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002250 Py_INCREF(Py_None);
2251 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002252}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002253#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002254
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002255
Guido van Rossumb6775db1994-08-01 11:34:53 +00002256#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002257static char posix_tcgetpgrp__doc__[] =
2258"tcgetpgrp(fd) -> pgid\n\
2259Return the process group associated with the terminal given by a fd.";
2260
Barry Warsaw53699e91996-12-10 23:23:01 +00002261static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002262posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002263 PyObject *self;
2264 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002265{
2266 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002267 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002268 return NULL;
2269 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002270 if (pgid < 0)
2271 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002272 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002273}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002274#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002275
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002276
Guido van Rossumb6775db1994-08-01 11:34:53 +00002277#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002278static char posix_tcsetpgrp__doc__[] =
2279"tcsetpgrp(fd, pgid) -> None\n\
2280Set the process group associated with the terminal given by a fd.";
2281
Barry Warsaw53699e91996-12-10 23:23:01 +00002282static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002283posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002284 PyObject *self;
2285 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002286{
2287 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002288 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002289 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002290 if (tcsetpgrp(fd, pgid) < 0)
2291 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002292 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002293 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002294}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002295#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002296
Guido van Rossum687dd131993-05-17 08:34:16 +00002297/* Functions acting on file descriptors */
2298
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002299static char posix_open__doc__[] =
2300"open(filename, flag [, mode=0777]) -> fd\n\
2301Open a file (for low level IO).";
2302
Barry Warsaw53699e91996-12-10 23:23:01 +00002303static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002304posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002305 PyObject *self;
2306 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002307{
2308 char *file;
2309 int flag;
2310 int mode = 0777;
2311 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002312 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2313 return NULL;
2314
Barry Warsaw53699e91996-12-10 23:23:01 +00002315 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002316 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002317 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002318 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002319 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002320 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002321}
2322
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002323
2324static char posix_close__doc__[] =
2325"close(fd) -> None\n\
2326Close a file descriptor (for low level IO).";
2327
Barry Warsaw53699e91996-12-10 23:23:01 +00002328static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002329posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002330 PyObject *self;
2331 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002332{
2333 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002334 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002335 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002336 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002337 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002338 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002339 if (res < 0)
2340 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002341 Py_INCREF(Py_None);
2342 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002343}
2344
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002345
2346static char posix_dup__doc__[] =
2347"dup(fd) -> fd2\n\
2348Return a duplicate of a file descriptor.";
2349
Barry Warsaw53699e91996-12-10 23:23:01 +00002350static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002351posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002352 PyObject *self;
2353 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002354{
2355 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002356 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002357 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002358 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002359 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002360 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002361 if (fd < 0)
2362 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002363 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002364}
2365
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002366
2367static char posix_dup2__doc__[] =
2368"dup2(fd, fd2) -> None\n\
2369Duplicate file descriptor.";
2370
Barry Warsaw53699e91996-12-10 23:23:01 +00002371static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002372posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002373 PyObject *self;
2374 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002375{
2376 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002377 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002378 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002379 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002380 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002381 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002382 if (res < 0)
2383 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002384 Py_INCREF(Py_None);
2385 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002386}
2387
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002388
2389static char posix_lseek__doc__[] =
2390"lseek(fd, pos, how) -> newpos\n\
2391Set the current position of a file descriptor.";
2392
Barry Warsaw53699e91996-12-10 23:23:01 +00002393static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002394posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002395 PyObject *self;
2396 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002397{
2398 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002399 off_t pos, res;
2400 PyObject *posobj;
2401 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002402 return NULL;
2403#ifdef SEEK_SET
2404 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2405 switch (how) {
2406 case 0: how = SEEK_SET; break;
2407 case 1: how = SEEK_CUR; break;
2408 case 2: how = SEEK_END; break;
2409 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002410#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002411
2412#if !defined(HAVE_LARGEFILE_SUPPORT)
2413 pos = PyInt_AsLong(posobj);
2414#else
2415 pos = PyLong_Check(posobj) ?
2416 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2417#endif
2418 if (PyErr_Occurred())
2419 return NULL;
2420
Barry Warsaw53699e91996-12-10 23:23:01 +00002421 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002422 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002423 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002424 if (res < 0)
2425 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002426
2427#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002428 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002429#else
2430 return PyLong_FromLongLong(res);
2431#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002432}
2433
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002434
2435static char posix_read__doc__[] =
2436"read(fd, buffersize) -> string\n\
2437Read a file descriptor.";
2438
Barry Warsaw53699e91996-12-10 23:23:01 +00002439static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002440posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002441 PyObject *self;
2442 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002443{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002444 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002445 PyObject *buffer;
2446 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002447 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002448 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002449 if (buffer == NULL)
2450 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002451 Py_BEGIN_ALLOW_THREADS
2452 n = read(fd, PyString_AsString(buffer), size);
2453 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002454 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002455 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002456 return posix_error();
2457 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002458 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002459 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002460 return buffer;
2461}
2462
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002463
2464static char posix_write__doc__[] =
2465"write(fd, string) -> byteswritten\n\
2466Write a string to a file descriptor.";
2467
Barry Warsaw53699e91996-12-10 23:23:01 +00002468static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002469posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002470 PyObject *self;
2471 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002472{
2473 int fd, size;
2474 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002475 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002476 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002477 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002478 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002479 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002480 if (size < 0)
2481 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002482 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002483}
2484
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002485
2486static char posix_fstat__doc__[]=
2487"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2488Like stat(), but for an open file descriptor.";
2489
Barry Warsaw53699e91996-12-10 23:23:01 +00002490static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002491posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002492 PyObject *self;
2493 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002494{
2495 int fd;
2496 struct stat st;
2497 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002498 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002499 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002500 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002501 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002502 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002503 if (res != 0)
2504 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002505#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002506 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002507 (long)st.st_mode,
2508 (long)st.st_ino,
2509 (long)st.st_dev,
2510 (long)st.st_nlink,
2511 (long)st.st_uid,
2512 (long)st.st_gid,
2513 (long)st.st_size,
2514 (long)st.st_atime,
2515 (long)st.st_mtime,
2516 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002517#else
2518 return Py_BuildValue("(lLllllLlll)",
2519 (long)st.st_mode,
2520 (LONG_LONG)st.st_ino,
2521 (long)st.st_dev,
2522 (long)st.st_nlink,
2523 (long)st.st_uid,
2524 (long)st.st_gid,
2525 (LONG_LONG)st.st_size,
2526 (long)st.st_atime,
2527 (long)st.st_mtime,
2528 (long)st.st_ctime);
2529#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002530}
2531
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002532
2533static char posix_fdopen__doc__[] =
2534"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2535Return an open file object connected to a file descriptor.";
2536
Barry Warsaw53699e91996-12-10 23:23:01 +00002537static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002538posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002539 PyObject *self;
2540 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002541{
Barry Warsaw53699e91996-12-10 23:23:01 +00002542 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002543 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002544 char *mode = "r";
2545 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002546 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002547 PyObject *f;
2548 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002549 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002550
Barry Warsaw53699e91996-12-10 23:23:01 +00002551 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002552 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002553 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002554 if (fp == NULL)
2555 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002556 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002557 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002558 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002559 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002560}
2561
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002562
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002563#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002564static char posix_pipe__doc__[] =
2565"pipe() -> (read_end, write_end)\n\
2566Create a pipe.";
2567
Barry Warsaw53699e91996-12-10 23:23:01 +00002568static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002569posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002570 PyObject *self;
2571 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002572{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002573#if defined(PYOS_OS2)
2574 HFILE read, write;
2575 APIRET rc;
2576
2577 if (!PyArg_Parse(args, ""))
2578 return NULL;
2579
2580 Py_BEGIN_ALLOW_THREADS
2581 rc = DosCreatePipe( &read, &write, 4096);
2582 Py_END_ALLOW_THREADS
2583 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002584 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002585
2586 return Py_BuildValue("(ii)", read, write);
2587#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002588#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002589 int fds[2];
2590 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002591 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002592 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002593 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002594 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002595 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002596 if (res != 0)
2597 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002598 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002599#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002600 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002601 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002602 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002603 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002604 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002605 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002606 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002607 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002608 if (!ok)
2609 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002610 read_fd = _open_osfhandle((long)read, 0);
2611 write_fd = _open_osfhandle((long)write, 1);
2612 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002613#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002614#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002615}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002616#endif /* HAVE_PIPE */
2617
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002618
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002619#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002620static char posix_mkfifo__doc__[] =
2621"mkfifo(file, [, mode=0666]) -> None\n\
2622Create a FIFO (a POSIX named pipe).";
2623
Barry Warsaw53699e91996-12-10 23:23:01 +00002624static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002625posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002626 PyObject *self;
2627 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002628{
2629 char *file;
2630 int mode = 0666;
2631 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002632 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002633 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002634 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002635 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002636 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002637 if (res < 0)
2638 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002639 Py_INCREF(Py_None);
2640 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002641}
2642#endif
2643
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002644
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002645#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002646static char posix_ftruncate__doc__[] =
2647"ftruncate(fd, length) -> None\n\
2648Truncate a file to a specified length.";
2649
Barry Warsaw53699e91996-12-10 23:23:01 +00002650static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002651posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002652 PyObject *self; /* Not used */
2653 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002654{
2655 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002656 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002657 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002658 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002659
Guido van Rossum94f6f721999-01-06 18:42:14 +00002660 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2661 return NULL;
2662
2663#if !defined(HAVE_LARGEFILE_SUPPORT)
2664 length = PyInt_AsLong(lenobj);
2665#else
2666 length = PyLong_Check(lenobj) ?
2667 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2668#endif
2669 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002670 return NULL;
2671
Barry Warsaw53699e91996-12-10 23:23:01 +00002672 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002673 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002674 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002675 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002676 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002677 return NULL;
2678 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002679 Py_INCREF(Py_None);
2680 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002681}
2682#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002683
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002684#ifdef NeXT
2685#define HAVE_PUTENV
2686/* Steve Spicklemire got this putenv from NeXTAnswers */
2687static int
2688putenv(char *newval)
2689{
2690 extern char **environ;
2691
2692 static int firstTime = 1;
2693 char **ep;
2694 char *cp;
2695 int esiz;
2696 char *np;
2697
2698 if (!(np = strchr(newval, '=')))
2699 return 1;
2700 *np = '\0';
2701
2702 /* look it up */
2703 for (ep=environ ; *ep ; ep++)
2704 {
2705 /* this should always be true... */
2706 if (cp = strchr(*ep, '='))
2707 {
2708 *cp = '\0';
2709 if (!strcmp(*ep, newval))
2710 {
2711 /* got it! */
2712 *cp = '=';
2713 break;
2714 }
2715 *cp = '=';
2716 }
2717 else
2718 {
2719 *np = '=';
2720 return 1;
2721 }
2722 }
2723
2724 *np = '=';
2725 if (*ep)
2726 {
2727 /* the string was already there:
2728 just replace it with the new one */
2729 *ep = newval;
2730 return 0;
2731 }
2732
2733 /* expand environ by one */
2734 for (esiz=2, ep=environ ; *ep ; ep++)
2735 esiz++;
2736 if (firstTime)
2737 {
2738 char **epp;
2739 char **newenv;
2740 if (!(newenv = malloc(esiz * sizeof(char *))))
2741 return 1;
2742
2743 for (ep=environ, epp=newenv ; *ep ;)
2744 *epp++ = *ep++;
2745 *epp++ = newval;
2746 *epp = (char *) 0;
2747 environ = newenv;
2748 }
2749 else
2750 {
2751 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2752 return 1;
2753 environ[esiz - 2] = newval;
2754 environ[esiz - 1] = (char *) 0;
2755 firstTime = 0;
2756 }
2757
2758 return 0;
2759}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002760#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002762
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002763#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002764static char posix_putenv__doc__[] =
2765"putenv(key, value) -> None\n\
2766Change or add an environment variable.";
2767
Guido van Rossumbcc20741998-08-04 22:53:56 +00002768#ifdef __BEOS__
2769/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2770int putenv( const char *str );
2771#endif
2772
Barry Warsaw53699e91996-12-10 23:23:01 +00002773static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002774posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002775 PyObject *self;
2776 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002777{
2778 char *s1, *s2;
2779 char *new;
2780
Barry Warsaw53699e91996-12-10 23:23:01 +00002781 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002782 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002783
2784#if defined(PYOS_OS2)
2785 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2786 APIRET rc;
2787
2788 if (strlen(s2) == 0) /* If New Value is an Empty String */
2789 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2790
2791 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2792 if (rc != NO_ERROR)
2793 return os2_error(rc);
2794
2795 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2796 APIRET rc;
2797
2798 if (strlen(s2) == 0) /* If New Value is an Empty String */
2799 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2800
2801 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2802 if (rc != NO_ERROR)
2803 return os2_error(rc);
2804 } else {
2805#endif
2806
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002807 /* XXX This leaks memory -- not easy to fix :-( */
2808 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002809 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002810 (void) sprintf(new, "%s=%s", s1, s2);
2811 if (putenv(new)) {
2812 posix_error();
2813 return NULL;
2814 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002815
2816#if defined(PYOS_OS2)
2817 }
2818#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002819 Py_INCREF(Py_None);
2820 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002821}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002822#endif /* putenv */
2823
2824#ifdef HAVE_STRERROR
2825static char posix_strerror__doc__[] =
2826"strerror(code) -> string\n\
2827Translate an error code to a message string.";
2828
2829PyObject *
2830posix_strerror(self, args)
2831 PyObject *self;
2832 PyObject *args;
2833{
2834 int code;
2835 char *message;
2836 if (!PyArg_ParseTuple(args, "i", &code))
2837 return NULL;
2838 message = strerror(code);
2839 if (message == NULL) {
2840 PyErr_SetString(PyExc_ValueError,
2841 "strerror code out of range");
2842 return NULL;
2843 }
2844 return PyString_FromString(message);
2845}
2846#endif /* strerror */
2847
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002848
Guido van Rossumc9641791998-08-04 15:26:23 +00002849#ifdef HAVE_SYS_WAIT_H
2850
2851#ifdef WIFSTOPPED
2852static char posix_WIFSTOPPED__doc__[] =
2853"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002854Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002855
2856static PyObject *
2857posix_WIFSTOPPED(self, args)
2858 PyObject *self;
2859 PyObject *args;
2860{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002861#ifdef UNION_WAIT
2862 union wait status;
2863#define status_i (status.w_status)
2864#else
2865 int status;
2866#define status_i status
2867#endif
2868 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002869
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002870 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002871 {
2872 return NULL;
2873 }
2874
2875 return Py_BuildValue("i", WIFSTOPPED(status));
2876}
2877#endif /* WIFSTOPPED */
2878
2879#ifdef WIFSIGNALED
2880static char posix_WIFSIGNALED__doc__[] =
2881"WIFSIGNALED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002882Retrun true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002883
2884static PyObject *
2885posix_WIFSIGNALED(self, args)
2886 PyObject *self;
2887 PyObject *args;
2888{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002889#ifdef UNION_WAIT
2890 union wait status;
2891#define status_i (status.w_status)
2892#else
2893 int status;
2894#define status_i status
2895#endif
2896 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002897
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002898 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002899 {
2900 return NULL;
2901 }
2902
2903 return Py_BuildValue("i", WIFSIGNALED(status));
2904}
2905#endif /* WIFSIGNALED */
2906
2907#ifdef WIFEXITED
2908static char posix_WIFEXITED__doc__[] =
2909"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002910Return true if the process returning 'status' exited using the exit()\n\
2911system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002912
2913static PyObject *
2914posix_WIFEXITED(self, args)
2915 PyObject *self;
2916 PyObject *args;
2917{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002918#ifdef UNION_WAIT
2919 union wait status;
2920#define status_i (status.w_status)
2921#else
2922 int status;
2923#define status_i status
2924#endif
2925 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002926
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002927 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002928 {
2929 return NULL;
2930 }
2931
2932 return Py_BuildValue("i", WIFEXITED(status));
2933}
2934#endif /* WIFEXITED */
2935
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002936#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00002937static char posix_WEXITSTATUS__doc__[] =
2938"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002939Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002940
2941static PyObject *
2942posix_WEXITSTATUS(self, args)
2943 PyObject *self;
2944 PyObject *args;
2945{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002946#ifdef UNION_WAIT
2947 union wait status;
2948#define status_i (status.w_status)
2949#else
2950 int status;
2951#define status_i status
2952#endif
2953 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002954
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002955 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002956 {
2957 return NULL;
2958 }
2959
2960 return Py_BuildValue("i", WEXITSTATUS(status));
2961}
2962#endif /* WEXITSTATUS */
2963
2964#ifdef WTERMSIG
2965static char posix_WTERMSIG__doc__[] =
2966"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002967Return the signal that terminated the process that provided the 'status'\n\
2968value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002969
2970static PyObject *
2971posix_WTERMSIG(self, args)
2972 PyObject *self;
2973 PyObject *args;
2974{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002975#ifdef UNION_WAIT
2976 union wait status;
2977#define status_i (status.w_status)
2978#else
2979 int status;
2980#define status_i status
2981#endif
2982 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002983
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002984 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002985 {
2986 return NULL;
2987 }
2988
2989 return Py_BuildValue("i", WTERMSIG(status));
2990}
2991#endif /* WTERMSIG */
2992
2993#ifdef WSTOPSIG
2994static char posix_WSTOPSIG__doc__[] =
2995"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002996Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002997
2998static PyObject *
2999posix_WSTOPSIG(self, args)
3000 PyObject *self;
3001 PyObject *args;
3002{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003003#ifdef UNION_WAIT
3004 union wait status;
3005#define status_i (status.w_status)
3006#else
3007 int status;
3008#define status_i status
3009#endif
3010 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003011
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003012 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003013 {
3014 return NULL;
3015 }
3016
3017 return Py_BuildValue("i", WSTOPSIG(status));
3018}
3019#endif /* WSTOPSIG */
3020
3021#endif /* HAVE_SYS_WAIT_H */
3022
3023
Guido van Rossum94f6f721999-01-06 18:42:14 +00003024#if defined(HAVE_FSTATVFS)
3025#include <sys/statvfs.h>
3026
3027static char posix_fstatvfs__doc__[] =
3028"fstatvfs(fd) -> \
3029(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
3030Perform an fstatvfs system call on the given fd.";
3031
3032static PyObject *
3033posix_fstatvfs(self, args)
3034 PyObject *self;
3035 PyObject *args;
3036{
3037 int fd, res;
3038 struct statvfs st;
3039 if (!PyArg_ParseTuple(args, "i", &fd))
3040 return NULL;
3041 Py_BEGIN_ALLOW_THREADS
3042 res = fstatvfs(fd, &st);
3043 Py_END_ALLOW_THREADS
3044 if (res != 0)
3045 return posix_error();
3046#if !defined(HAVE_LARGEFILE_SUPPORT)
3047 return Py_BuildValue("(lllllllllll)",
3048 (long) st.f_bsize,
3049 (long) st.f_frsize,
3050 (long) st.f_blocks,
3051 (long) st.f_bfree,
3052 (long) st.f_bavail,
3053 (long) st.f_files,
3054 (long) st.f_ffree,
3055 (long) st.f_favail,
3056 (long) st.f_fsid,
3057 (long) st.f_flag,
3058 (long) st.f_namemax);
3059#else
3060 return Py_BuildValue("(llLLLLLLlll)",
3061 (long) st.f_bsize,
3062 (long) st.f_frsize,
3063 (LONG_LONG) st.f_blocks,
3064 (LONG_LONG) st.f_bfree,
3065 (LONG_LONG) st.f_bavail,
3066 (LONG_LONG) st.f_files,
3067 (LONG_LONG) st.f_ffree,
3068 (LONG_LONG) st.f_favail,
3069 (long) st.f_fsid,
3070 (long) st.f_flag,
3071 (long) st.f_namemax);
3072#endif
3073}
3074#endif /* HAVE_FSTATVFS */
3075
3076
3077#if defined(HAVE_STATVFS)
3078#include <sys/statvfs.h>
3079
3080static char posix_statvfs__doc__[] =
3081"statvfs(path) -> \
3082(bsize,frsize,blocks,bfree,bavail,files,ffree,favail,fsid,flag, namemax)\n\
3083Perform a statvfs system call on the given path.";
3084
3085static PyObject *
3086posix_statvfs(self, args)
3087 PyObject *self;
3088 PyObject *args;
3089{
3090 char *path;
3091 int res;
3092 struct statvfs st;
3093 if (!PyArg_ParseTuple(args, "s", &path))
3094 return NULL;
3095 Py_BEGIN_ALLOW_THREADS
3096 res = statvfs(path, &st);
3097 Py_END_ALLOW_THREADS
3098 if (res != 0)
3099 return posix_error_with_filename(path);
3100#if !defined(HAVE_LARGEFILE_SUPPORT)
3101 return Py_BuildValue("(lllllllllll)",
3102 (long) st.f_bsize,
3103 (long) st.f_frsize,
3104 (long) st.f_blocks,
3105 (long) st.f_bfree,
3106 (long) st.f_bavail,
3107 (long) st.f_files,
3108 (long) st.f_ffree,
3109 (long) st.f_favail,
3110 (long) st.f_fsid,
3111 (long) st.f_flag,
3112 (long) st.f_namemax);
3113#else /* HAVE_LARGEFILE_SUPPORT */
3114 return Py_BuildValue("(llLLLLLLlll)",
3115 (long) st.f_bsize,
3116 (long) st.f_frsize,
3117 (LONG_LONG) st.f_blocks,
3118 (LONG_LONG) st.f_bfree,
3119 (LONG_LONG) st.f_bavail,
3120 (LONG_LONG) st.f_files,
3121 (LONG_LONG) st.f_ffree,
3122 (LONG_LONG) st.f_favail,
3123 (long) st.f_fsid,
3124 (long) st.f_flag,
3125 (long) st.f_namemax);
3126#endif
3127}
3128#endif /* HAVE_STATVFS */
3129
3130
Barry Warsaw53699e91996-12-10 23:23:01 +00003131static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00003132 {"access", posix_access, 0, posix_access__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003133#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +00003134 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003135#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003136 {"chdir", posix_chdir, 0, posix_chdir__doc__},
3137 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003138#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003139 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003140#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00003141#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003142 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00003143#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00003144#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003145 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003146#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003147 {"listdir", posix_listdir, 0, posix_listdir__doc__},
3148 {"lstat", posix_lstat, 0, posix_lstat__doc__},
3149 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003150#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003151 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003152#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003153#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003154 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003155#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003156 {"rename", posix_rename, 0, posix_rename__doc__},
3157 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
3158 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003159#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003160 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003161#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003162#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003163 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003164#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003165 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003166#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003167 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003168#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003169 {"unlink", posix_unlink, 0, posix_unlink__doc__},
3170 {"remove", posix_unlink, 0, posix_remove__doc__},
3171 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003172#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003173 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003174#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003175 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003176#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003177 {"execv", posix_execv, 0, posix_execv__doc__},
3178 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003179#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00003180#ifdef HAVE_SPAWNV
3181 {"spawnv", posix_spawnv, 0, posix_spawnv__doc__},
3182 {"spawnve", posix_spawnve, 0, posix_spawnve__doc__},
3183#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003184#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003185 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003186#endif /* HAVE_FORK */
3187#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003188 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003189#endif /* HAVE_GETEGID */
3190#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003191 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003192#endif /* HAVE_GETEUID */
3193#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003194 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003195#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003196 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003197#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003198 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003199#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003200#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003201 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003202#endif /* HAVE_GETPPID */
3203#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003204 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003205#endif /* HAVE_GETUID */
3206#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003207 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003208#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00003209#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003210 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00003211#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003212#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003213 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003214#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003215#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003216 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003217#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003218#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003219 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003220#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003221#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003222 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003223#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003224#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003225 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003226#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003227#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003228 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003229#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003230#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003231 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003232#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003233#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003234 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003235#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003236#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003237 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003238#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003239#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003240 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003241#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003242 {"open", posix_open, 1, posix_open__doc__},
3243 {"close", posix_close, 0, posix_close__doc__},
3244 {"dup", posix_dup, 0, posix_dup__doc__},
3245 {"dup2", posix_dup2, 0, posix_dup2__doc__},
3246 {"lseek", posix_lseek, 0, posix_lseek__doc__},
3247 {"read", posix_read, 0, posix_read__doc__},
3248 {"write", posix_write, 0, posix_write__doc__},
3249 {"fstat", posix_fstat, 0, posix_fstat__doc__},
3250 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003251#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003252 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003253#endif
3254#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003255 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003256#endif
3257#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003258 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003259#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003260#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003261 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003262#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00003263#ifdef HAVE_STRERROR
3264 {"strerror", posix_strerror, 1, posix_strerror__doc__},
3265#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00003266#ifdef HAVE_FSYNC
3267 {"fsync", posix_fsync, 0, posix_fsync__doc__},
3268#endif
3269#ifdef HAVE_FDATASYNC
3270 {"fdatasync", posix_fdatasync, 0, posix_fdatasync__doc__},
3271#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00003272#ifdef HAVE_SYS_WAIT_H
3273#ifdef WIFSTOPPED
3274 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
3275#endif /* WIFSTOPPED */
3276#ifdef WIFSIGNALED
3277 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
3278#endif /* WIFSIGNALED */
3279#ifdef WIFEXITED
3280 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
3281#endif /* WIFEXITED */
3282#ifdef WEXITSTATUS
3283 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
3284#endif /* WEXITSTATUS */
3285#ifdef WTERMSIG
3286 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
3287#endif /* WTERMSIG */
3288#ifdef WSTOPSIG
3289 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
3290#endif /* WSTOPSIG */
3291#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003292#ifdef HAVE_FSTATVFS
3293 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
3294#endif
3295#ifdef HAVE_STATVFS
3296 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
3297#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003298 {NULL, NULL} /* Sentinel */
3299};
3300
3301
Barry Warsaw4a342091996-12-19 23:50:02 +00003302static int
3303ins(d, symbol, value)
3304 PyObject* d;
3305 char* symbol;
3306 long value;
3307{
3308 PyObject* v = PyInt_FromLong(value);
3309 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
3310 return -1; /* triggers fatal error */
3311
3312 Py_DECREF(v);
3313 return 0;
3314}
3315
Guido van Rossumd48f2521997-12-05 22:19:34 +00003316#if defined(PYOS_OS2)
3317/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
3318static int insertvalues(PyObject *d)
3319{
3320 APIRET rc;
3321 ULONG values[QSV_MAX+1];
3322 PyObject *v;
3323 char *ver, tmp[10];
3324
3325 Py_BEGIN_ALLOW_THREADS
3326 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
3327 Py_END_ALLOW_THREADS
3328
3329 if (rc != NO_ERROR) {
3330 os2_error(rc);
3331 return -1;
3332 }
3333
3334 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3335 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3336 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3337 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3338 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3339 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3340 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3341
3342 switch (values[QSV_VERSION_MINOR]) {
3343 case 0: ver = "2.00"; break;
3344 case 10: ver = "2.10"; break;
3345 case 11: ver = "2.11"; break;
3346 case 30: ver = "3.00"; break;
3347 case 40: ver = "4.00"; break;
3348 case 50: ver = "5.00"; break;
3349 default:
3350 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3351 values[QSV_VERSION_MINOR]);
3352 ver = &tmp[0];
3353 }
3354
3355 /* Add Indicator of the Version of the Operating System */
3356 v = PyString_FromString(ver);
3357 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3358 return -1;
3359 Py_DECREF(v);
3360
3361 /* Add Indicator of Which Drive was Used to Boot the System */
3362 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3363 tmp[1] = ':';
3364 tmp[2] = '\0';
3365
3366 v = PyString_FromString(tmp);
3367 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3368 return -1;
3369 Py_DECREF(v);
3370
3371 return 0;
3372}
3373#endif
3374
Barry Warsaw4a342091996-12-19 23:50:02 +00003375static int
3376all_ins(d)
3377 PyObject* d;
3378{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003379#ifdef F_OK
3380 if (ins(d, "F_OK", (long)F_OK)) return -1;
3381#endif
3382#ifdef R_OK
3383 if (ins(d, "R_OK", (long)R_OK)) return -1;
3384#endif
3385#ifdef W_OK
3386 if (ins(d, "W_OK", (long)W_OK)) return -1;
3387#endif
3388#ifdef X_OK
3389 if (ins(d, "X_OK", (long)X_OK)) return -1;
3390#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003391#ifdef WNOHANG
3392 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3393#endif
3394#ifdef O_RDONLY
3395 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3396#endif
3397#ifdef O_WRONLY
3398 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3399#endif
3400#ifdef O_RDWR
3401 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3402#endif
3403#ifdef O_NDELAY
3404 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3405#endif
3406#ifdef O_NONBLOCK
3407 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3408#endif
3409#ifdef O_APPEND
3410 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3411#endif
3412#ifdef O_DSYNC
3413 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3414#endif
3415#ifdef O_RSYNC
3416 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3417#endif
3418#ifdef O_SYNC
3419 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3420#endif
3421#ifdef O_NOCTTY
3422 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3423#endif
3424#ifdef O_CREAT
3425 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3426#endif
3427#ifdef O_EXCL
3428 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3429#endif
3430#ifdef O_TRUNC
3431 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3432#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003433#ifdef O_BINARY
3434 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3435#endif
3436#ifdef O_TEXT
3437 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3438#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003439
Guido van Rossum246bc171999-02-01 23:54:31 +00003440#ifdef HAVE_SPAWNV
3441 if (ins(d, "_P_WAIT", (long)_P_WAIT)) return -1;
3442 if (ins(d, "_P_NOWAIT", (long)_P_NOWAIT)) return -1;
3443 if (ins(d, "_P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
3444 if (ins(d, "_P_NOWAITO", (long)_P_NOWAITO)) return -1;
3445 if (ins(d, "_P_DETACH", (long)_P_DETACH)) return -1;
3446#endif
3447
Guido van Rossumd48f2521997-12-05 22:19:34 +00003448#if defined(PYOS_OS2)
3449 if (insertvalues(d)) return -1;
3450#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003451 return 0;
3452}
3453
3454
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003455#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003456#define INITFUNC initnt
3457#define MODNAME "nt"
3458#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003459#if defined(PYOS_OS2)
3460#define INITFUNC initos2
3461#define MODNAME "os2"
3462#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003463#define INITFUNC initposix
3464#define MODNAME "posix"
3465#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003466#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003467
Guido van Rossum3886bb61998-12-04 18:50:17 +00003468DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003469INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003470{
Barry Warsaw53699e91996-12-10 23:23:01 +00003471 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003472
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003473 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003474 posix_methods,
3475 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003476 (PyObject *)NULL,
3477 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003478 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003479
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003480 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003481 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003482 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003483 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003484 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003485
Barry Warsaw4a342091996-12-19 23:50:02 +00003486 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003487 return;
3488
Barry Warsawd58d7641998-07-23 16:14:40 +00003489 Py_INCREF(PyExc_OSError);
3490 PosixError = PyExc_OSError;
3491 PyDict_SetItemString(d, "error", PosixError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003492}