blob: 013e3b17d367d03951ef3d36e2d668299c032778 [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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000329/* Set a POSIX-specific error from errno, and return NULL */
330
Barry Warsawd58d7641998-07-23 16:14:40 +0000331static PyObject *
332posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000333{
Barry Warsawca74da41999-02-09 19:31:45 +0000334 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000335}
Barry Warsawd58d7641998-07-23 16:14:40 +0000336static PyObject *
337posix_error_with_filename(name)
338 char* name;
339{
Barry Warsawca74da41999-02-09 19:31:45 +0000340 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000341}
342
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000343
Guido van Rossumd48f2521997-12-05 22:19:34 +0000344#if defined(PYOS_OS2)
345/**********************************************************************
346 * Helper Function to Trim and Format OS/2 Messages
347 **********************************************************************/
348 static void
349os2_formatmsg(char *msgbuf, int msglen, char *reason)
350{
351 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
352
353 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
354 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
355
356 while (lastc > msgbuf && isspace(*lastc))
357 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
358 }
359
360 /* Add Optional Reason Text */
361 if (reason) {
362 strcat(msgbuf, " : ");
363 strcat(msgbuf, reason);
364 }
365}
366
367/**********************************************************************
368 * Decode an OS/2 Operating System Error Code
369 *
370 * A convenience function to lookup an OS/2 error code and return a
371 * text message we can use to raise a Python exception.
372 *
373 * Notes:
374 * The messages for errors returned from the OS/2 kernel reside in
375 * the file OSO001.MSG in the \OS2 directory hierarchy.
376 *
377 **********************************************************************/
378 static char *
379os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
380{
381 APIRET rc;
382 ULONG msglen;
383
384 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
385 Py_BEGIN_ALLOW_THREADS
386 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
387 errorcode, "oso001.msg", &msglen);
388 Py_END_ALLOW_THREADS
389
390 if (rc == NO_ERROR)
391 os2_formatmsg(msgbuf, msglen, reason);
392 else
393 sprintf(msgbuf, "unknown OS error #%d", errorcode);
394
395 return msgbuf;
396}
397
398/* Set an OS/2-specific error and return NULL. OS/2 kernel
399 errors are not in a global variable e.g. 'errno' nor are
400 they congruent with posix error numbers. */
401
402static PyObject * os2_error(int code)
403{
404 char text[1024];
405 PyObject *v;
406
407 os2_strerror(text, sizeof(text), code, "");
408
409 v = Py_BuildValue("(is)", code, text);
410 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000411 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000412 Py_DECREF(v);
413 }
414 return NULL; /* Signal to Python that an Exception is Pending */
415}
416
417#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000418
419/* POSIX generic methods */
420
Barry Warsaw53699e91996-12-10 23:23:01 +0000421static PyObject *
Guido van Rossum21142a01999-01-08 21:05:37 +0000422posix_int(args, func)
423 PyObject *args;
424 int (*func) Py_FPROTO((int));
425{
426 int fd;
427 int res;
428 if (!PyArg_Parse(args, "i", &fd))
429 return NULL;
430 Py_BEGIN_ALLOW_THREADS
431 res = (*func)(fd);
432 Py_END_ALLOW_THREADS
433 if (res < 0)
434 return posix_error();
435 Py_INCREF(Py_None);
436 return Py_None;
437}
438
439
440static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000441posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000442 PyObject *args;
443 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000445 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000446 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000447 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000449 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000450 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000451 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000452 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000453 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000454 Py_INCREF(Py_None);
455 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456}
457
Barry Warsaw53699e91996-12-10 23:23:01 +0000458static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000460 PyObject *args;
461 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000462{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000463 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000464 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000465 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000468 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000469 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000470 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000471 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000473 Py_INCREF(Py_None);
474 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000475}
476
Barry Warsaw53699e91996-12-10 23:23:01 +0000477static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000479 PyObject *args;
480 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000482 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000484 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000485 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000487 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000488 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000489 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000490 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000491 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000492 Py_INCREF(Py_None);
493 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000494}
495
Barry Warsaw53699e91996-12-10 23:23:01 +0000496static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000497posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000498 PyObject *args;
499 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000500{
501 char *path;
502 int i,i2;
503 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000504 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000505 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000506 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000507 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000508 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000509 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000510 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000511 Py_INCREF(Py_None);
512 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000513}
514
Barry Warsaw53699e91996-12-10 23:23:01 +0000515static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000516posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000517 PyObject *self;
518 PyObject *args;
519 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000520{
521 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000522 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000523 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000524 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000525 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000526 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000527 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000528 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000529 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000530 return posix_error_with_filename(path);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000531#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000532 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000533 (long)st.st_mode,
534 (long)st.st_ino,
535 (long)st.st_dev,
536 (long)st.st_nlink,
537 (long)st.st_uid,
538 (long)st.st_gid,
539 (long)st.st_size,
540 (long)st.st_atime,
541 (long)st.st_mtime,
542 (long)st.st_ctime);
543#else
544 return Py_BuildValue("(lLllllLlll)",
545 (long)st.st_mode,
546 (LONG_LONG)st.st_ino,
547 (long)st.st_dev,
548 (long)st.st_nlink,
549 (long)st.st_uid,
550 (long)st.st_gid,
551 (LONG_LONG)st.st_size,
552 (long)st.st_atime,
553 (long)st.st_mtime,
554 (long)st.st_ctime);
555#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000556}
557
558
559/* POSIX methods */
560
Guido van Rossum94f6f721999-01-06 18:42:14 +0000561static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000562"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000563Test for access to a file.";
564
565static PyObject *
566posix_access(self, args)
567 PyObject *self;
568 PyObject *args;
569{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000570 char *path;
571 int mode;
572 int res;
573
574 if (!PyArg_Parse(args, "(si)", &path, &mode))
575 return NULL;
576 Py_BEGIN_ALLOW_THREADS
577 res = access(path, mode);
578 Py_END_ALLOW_THREADS
579 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000580}
581
Guido van Rossumd371ff11999-01-25 16:12:23 +0000582#ifndef F_OK
583#define F_OK 0
584#endif
585#ifndef R_OK
586#define R_OK 4
587#endif
588#ifndef W_OK
589#define W_OK 2
590#endif
591#ifndef X_OK
592#define X_OK 1
593#endif
594
595#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000596static char posix_ttyname__doc__[] =
597"ttyname(fd, mode) -> String\n\
598Return the name of the terminal device connected to 'fd'.";
599
600static PyObject *
601posix_ttyname(self, args)
602 PyObject *self;
603 PyObject *args;
604{
605 PyObject *file;
606 int id;
607 char *ret;
608
Guido van Rossum94f6f721999-01-06 18:42:14 +0000609 if (!PyArg_Parse(args, "i", &id))
610 return NULL;
611
Guido van Rossum94f6f721999-01-06 18:42:14 +0000612 ret = ttyname(id);
613 if (ret == NULL)
614 return(posix_error());
615 return(PyString_FromString(ret));
616}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000617#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000618
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000619static char posix_chdir__doc__[] =
620"chdir(path) -> None\n\
621Change the current working directory to the specified path.";
622
Barry Warsaw53699e91996-12-10 23:23:01 +0000623static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000624posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000625 PyObject *self;
626 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000627{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000628 return posix_1str(args, chdir);
629}
630
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000631
632static char posix_chmod__doc__[] =
633"chmod(path, mode) -> None\n\
634Change the access permissions of a file.";
635
Barry Warsaw53699e91996-12-10 23:23:01 +0000636static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000637posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000638 PyObject *self;
639 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000640{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641 return posix_strint(args, chmod);
642}
643
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000644
Guido van Rossum21142a01999-01-08 21:05:37 +0000645#ifdef HAVE_FSYNC
646static char posix_fsync__doc__[] =
647"fsync(fildes) -> None\n\
648force write of file with filedescriptor to disk.";
649
650static PyObject *
651posix_fsync(self, args)
652 PyObject *self;
653 PyObject *args;
654{
Guido van Rossum21142a01999-01-08 21:05:37 +0000655 return posix_int(args, fsync);
656}
657#endif /* HAVE_FSYNC */
658
659#ifdef HAVE_FDATASYNC
660static char posix_fdatasync__doc__[] =
661"fdatasync(fildes) -> None\n\
662force write of file with filedescriptor to disk.\n\
663 does not force update of metadata.";
664
Guido van Rossum5d00b6d1999-01-08 21:28:05 +0000665extern int fdatasync(int); /* Prototype just in case */
666
Guido van Rossum21142a01999-01-08 21:05:37 +0000667static PyObject *
668posix_fdatasync(self, args)
669 PyObject *self;
670 PyObject *args;
671{
Guido van Rossum21142a01999-01-08 21:05:37 +0000672 return posix_int(args, fdatasync);
673}
674#endif /* HAVE_FDATASYNC */
675
676
Guido van Rossumb6775db1994-08-01 11:34:53 +0000677#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000678static char posix_chown__doc__[] =
679"chown(path, uid, gid) -> None\n\
680Change the owner and group id of path to the numeric uid and gid.";
681
Barry Warsaw53699e91996-12-10 23:23:01 +0000682static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000683posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000684 PyObject *self;
685 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000686{
687 return posix_strintint(args, chown);
688}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000689#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000690
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000691
Guido van Rossum36bc6801995-06-14 22:54:23 +0000692#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000693static char posix_getcwd__doc__[] =
694"getcwd() -> path\n\
695Return a string representing the current working directory.";
696
Barry Warsaw53699e91996-12-10 23:23:01 +0000697static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000698posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000699 PyObject *self;
700 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000701{
702 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000703 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000704 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000705 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000706 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000707 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000708 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000709 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000710 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000711 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000712}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000713#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000715
Guido van Rossumb6775db1994-08-01 11:34:53 +0000716#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000717static char posix_link__doc__[] =
718"link(src, dst) -> None\n\
719Create a hard link to a file.";
720
Barry Warsaw53699e91996-12-10 23:23:01 +0000721static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000723 PyObject *self;
724 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000725{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000726 return posix_2str(args, link);
727}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000728#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000729
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000730
731static char posix_listdir__doc__[] =
732"listdir(path) -> list_of_strings\n\
733Return a list containing the names of the entries in the directory.\n\
734\n\
735 path: path of directory to list\n\
736\n\
737The list is in arbitrary order. It does not include the special\n\
738entries '.' and '..' even if they are present in the directory.";
739
Barry Warsaw53699e91996-12-10 23:23:01 +0000740static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000741posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000742 PyObject *self;
743 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000744{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000745 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000746 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000747#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000748
Guido van Rossumb6775db1994-08-01 11:34:53 +0000749 char *name;
750 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000751 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000752 HANDLE hFindFile;
753 WIN32_FIND_DATA FileData;
754 char namebuf[MAX_PATH+5];
755
Guido van Rossum7e488981998-10-08 02:25:24 +0000756 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000757 return NULL;
758 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000759 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000760 return NULL;
761 }
762 strcpy(namebuf, name);
763 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
764 namebuf[len++] = '/';
765 strcpy(namebuf + len, "*.*");
766
Barry Warsaw53699e91996-12-10 23:23:01 +0000767 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000768 return NULL;
769
770 hFindFile = FindFirstFile(namebuf, &FileData);
771 if (hFindFile == INVALID_HANDLE_VALUE) {
772 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000773 if (errno == ERROR_FILE_NOT_FOUND)
774 return PyList_New(0);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000775 return posix_error();
776 }
777 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000778 if (FileData.cFileName[0] == '.' &&
779 (FileData.cFileName[1] == '\0' ||
780 FileData.cFileName[1] == '.' &&
781 FileData.cFileName[2] == '\0'))
782 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000783 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000784 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000785 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000786 d = NULL;
787 break;
788 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000789 if (PyList_Append(d, v) != 0) {
790 Py_DECREF(v);
791 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000792 d = NULL;
793 break;
794 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000795 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000796 } while (FindNextFile(hFindFile, &FileData) == TRUE);
797
798 if (FindClose(hFindFile) == FALSE) {
799 errno = GetLastError();
800 return posix_error();
801 }
802
803 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000804
Guido van Rossum8d665e61996-06-26 18:22:49 +0000805#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000806#ifdef _MSC_VER /* 16-bit Windows */
807
808#ifndef MAX_PATH
809#define MAX_PATH 250
810#endif
811 char *name, *pt;
812 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000813 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000814 char namebuf[MAX_PATH+5];
815 struct _find_t ep;
816
Guido van Rossum7e488981998-10-08 02:25:24 +0000817 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000818 return NULL;
819 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000820 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000821 return NULL;
822 }
823 strcpy(namebuf, name);
824 for (pt = namebuf; *pt; pt++)
825 if (*pt == '/')
826 *pt = '\\';
827 if (namebuf[len-1] != '\\')
828 namebuf[len++] = '\\';
829 strcpy(namebuf + len, "*.*");
830
Barry Warsaw53699e91996-12-10 23:23:01 +0000831 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000832 return NULL;
833
834 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000835 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
836 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000837 errno = ENOENT;
838 return posix_error();
839 }
840 do {
841 if (ep.name[0] == '.' &&
842 (ep.name[1] == '\0' ||
843 ep.name[1] == '.' &&
844 ep.name[2] == '\0'))
845 continue;
846 strcpy(namebuf, ep.name);
847 for (pt = namebuf; *pt; pt++)
848 if (isupper(*pt))
849 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000850 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000851 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000852 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000853 d = NULL;
854 break;
855 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000856 if (PyList_Append(d, v) != 0) {
857 Py_DECREF(v);
858 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000859 d = NULL;
860 break;
861 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000862 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000863 } while (_dos_findnext(&ep) == 0);
864
865 return d;
866
867#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000868#if defined(PYOS_OS2)
869
870#ifndef MAX_PATH
871#define MAX_PATH CCHMAXPATH
872#endif
873 char *name, *pt;
874 int len;
875 PyObject *d, *v;
876 char namebuf[MAX_PATH+5];
877 HDIR hdir = 1;
878 ULONG srchcnt = 1;
879 FILEFINDBUF3 ep;
880 APIRET rc;
881
Guido van Rossum7e488981998-10-08 02:25:24 +0000882 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000883 return NULL;
884 if (len >= MAX_PATH) {
885 PyErr_SetString(PyExc_ValueError, "path too long");
886 return NULL;
887 }
888 strcpy(namebuf, name);
889 for (pt = namebuf; *pt; pt++)
890 if (*pt == '/')
891 *pt = '\\';
892 if (namebuf[len-1] != '\\')
893 namebuf[len++] = '\\';
894 strcpy(namebuf + len, "*.*");
895
896 if ((d = PyList_New(0)) == NULL)
897 return NULL;
898
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000899 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
900 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000901 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000902 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
903 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
904 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000905
906 if (rc != NO_ERROR) {
907 errno = ENOENT;
908 return posix_error();
909 }
910
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000911 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000912 do {
913 if (ep.achName[0] == '.'
914 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000915 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000916
917 strcpy(namebuf, ep.achName);
918
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000919 /* Leave Case of Name Alone -- In Native Form */
920 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000921
922 v = PyString_FromString(namebuf);
923 if (v == NULL) {
924 Py_DECREF(d);
925 d = NULL;
926 break;
927 }
928 if (PyList_Append(d, v) != 0) {
929 Py_DECREF(v);
930 Py_DECREF(d);
931 d = NULL;
932 break;
933 }
934 Py_DECREF(v);
935 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
936 }
937
938 return d;
939#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000940
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000941 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000942 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000943 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000944 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000945 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000946 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000947 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000948 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000949 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000950 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000951 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000952 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000953 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000954 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000955 return NULL;
956 }
957 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000958 if (ep->d_name[0] == '.' &&
959 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000960 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000961 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000962 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000963 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000964 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965 d = NULL;
966 break;
967 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000968 if (PyList_Append(d, v) != 0) {
969 Py_DECREF(v);
970 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000971 d = NULL;
972 break;
973 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000974 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000975 }
976 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000977 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000978
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000979 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000980
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000981#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000982#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000983#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000984}
985
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000986static char posix_mkdir__doc__[] =
987"mkdir(path [, mode=0777]) -> None\n\
988Create a directory.";
989
Barry Warsaw53699e91996-12-10 23:23:01 +0000990static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000991posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000992 PyObject *self;
993 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000994{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000995 int res;
996 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000997 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +0000998 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +0000999 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001000 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001001#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001002 res = mkdir(path);
1003#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001004 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001005#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001006 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001007 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001008 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001009 Py_INCREF(Py_None);
1010 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001011}
1012
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001013
Guido van Rossumb6775db1994-08-01 11:34:53 +00001014#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001015static char posix_nice__doc__[] =
1016"nice(inc) -> new_priority\n\
1017Decrease the priority of process and return new priority.";
1018
Barry Warsaw53699e91996-12-10 23:23:01 +00001019static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +00001020posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001021 PyObject *self;
1022 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001023{
1024 int increment, value;
1025
Barry Warsaw53699e91996-12-10 23:23:01 +00001026 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001027 return NULL;
1028 value = nice(increment);
1029 if (value == -1)
1030 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001031 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001032}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001033#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001034
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001035
1036static char posix_rename__doc__[] =
1037"rename(old, new) -> None\n\
1038Rename a file or directory.";
1039
Barry Warsaw53699e91996-12-10 23:23:01 +00001040static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001041posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001042 PyObject *self;
1043 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001044{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001045 return posix_2str(args, rename);
1046}
1047
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001048
1049static char posix_rmdir__doc__[] =
1050"rmdir(path) -> None\n\
1051Remove a directory.";
1052
Barry Warsaw53699e91996-12-10 23:23:01 +00001053static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001054posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001055 PyObject *self;
1056 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001057{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001058 return posix_1str(args, rmdir);
1059}
1060
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001061
1062static char posix_stat__doc__[] =
1063"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1064Perform a stat system call on the given path.";
1065
Barry Warsaw53699e91996-12-10 23:23:01 +00001066static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001067posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001068 PyObject *self;
1069 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001070{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001071 return posix_do_stat(self, args, stat);
1072}
1073
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001074
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001075#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001076static char posix_system__doc__[] =
1077"system(command) -> exit_status\n\
1078Execute the command (a string) in a subshell.";
1079
Barry Warsaw53699e91996-12-10 23:23:01 +00001080static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001081posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001082 PyObject *self;
1083 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001084{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001085 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001086 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001087 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001088 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001089 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001090 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001091 Py_END_ALLOW_THREADS
1092 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001093}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001094#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001095
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001096
1097static char posix_umask__doc__[] =
1098"umask(new_mask) -> old_mask\n\
1099Set the current numeric umask and return the previous umask.";
1100
Barry Warsaw53699e91996-12-10 23:23:01 +00001101static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001102posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001103 PyObject *self;
1104 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001105{
1106 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +00001107 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001108 return NULL;
1109 i = umask(i);
1110 if (i < 0)
1111 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001112 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001113}
1114
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001115
1116static char posix_unlink__doc__[] =
1117"unlink(path) -> None\n\
1118Remove a file (same as remove(path)).";
1119
1120static char posix_remove__doc__[] =
1121"remove(path) -> None\n\
1122Remove a file (same as unlink(path)).";
1123
Barry Warsaw53699e91996-12-10 23:23:01 +00001124static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001125posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001126 PyObject *self;
1127 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001128{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001129 return posix_1str(args, unlink);
1130}
1131
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001132
Guido van Rossumb6775db1994-08-01 11:34:53 +00001133#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001134static char posix_uname__doc__[] =
1135"uname() -> (sysname, nodename, release, version, machine)\n\
1136Return a tuple identifying the current operating system.";
1137
Barry Warsaw53699e91996-12-10 23:23:01 +00001138static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001139posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001140 PyObject *self;
1141 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001142{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001143 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001144 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001145 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001146 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001147 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001148 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001149 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001150 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001151 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001152 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001153 u.sysname,
1154 u.nodename,
1155 u.release,
1156 u.version,
1157 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001158}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001159#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001160
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001161
1162static char posix_utime__doc__[] =
1163"utime(path, (atime, utime)) -> None\n\
1164Set the access and modified time of the file to the given values.";
1165
Barry Warsaw53699e91996-12-10 23:23:01 +00001166static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001167posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001168 PyObject *self;
1169 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001170{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001171 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001172 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001173 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001174
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001175/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001176#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001177 struct utimbuf buf;
1178#define ATIME buf.actime
1179#define MTIME buf.modtime
1180#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001181#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001182 time_t buf[2];
1183#define ATIME buf[0]
1184#define MTIME buf[1]
1185#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001186#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001187
Barry Warsaw53699e91996-12-10 23:23:01 +00001188 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001189 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001190 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001191 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001192 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001193 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001194 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001195 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001196 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001197 Py_INCREF(Py_None);
1198 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001199#undef UTIME_ARG
1200#undef ATIME
1201#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001202}
1203
Guido van Rossum85e3b011991-06-03 12:42:10 +00001204
Guido van Rossum3b066191991-06-04 19:40:25 +00001205/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001206
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001207static char posix__exit__doc__[] =
1208"_exit(status)\n\
1209Exit to the system with specified status, without normal exit processing.";
1210
Barry Warsaw53699e91996-12-10 23:23:01 +00001211static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001212posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001213 PyObject *self;
1214 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001215{
1216 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001217 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001218 return NULL;
1219 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001220 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001221}
1222
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001223
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001224#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001225static char posix_execv__doc__[] =
1226"execv(path, args)\n\
1227Execute an executable path with arguments, replacing current process.\n\
1228\n\
1229 path: path of executable file\n\
1230 args: tuple or list of strings";
1231
Barry Warsaw53699e91996-12-10 23:23:01 +00001232static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001233posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001234 PyObject *self;
1235 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001236{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001237 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001238 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001239 char **argvlist;
1240 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001241 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001242
Guido van Rossum89b33251993-10-22 14:26:06 +00001243 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001244 argv is a list or tuple of strings. */
1245
Barry Warsaw53699e91996-12-10 23:23:01 +00001246 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001247 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001248 if (PyList_Check(argv)) {
1249 argc = PyList_Size(argv);
1250 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001251 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001252 else if (PyTuple_Check(argv)) {
1253 argc = PyTuple_Size(argv);
1254 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001255 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001256 else {
1257 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001258 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001259 return NULL;
1260 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001261
Barry Warsaw53699e91996-12-10 23:23:01 +00001262 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001263 if (argvlist == NULL)
1264 return NULL;
1265 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001266 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1267 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001268 goto badarg;
1269 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001270 }
1271 argvlist[argc] = NULL;
1272
Guido van Rossumb6775db1994-08-01 11:34:53 +00001273#ifdef BAD_EXEC_PROTOTYPES
1274 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001275#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001276 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001277#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001278
Guido van Rossum85e3b011991-06-03 12:42:10 +00001279 /* If we get here it's definitely an error */
1280
Barry Warsaw53699e91996-12-10 23:23:01 +00001281 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001282 return posix_error();
1283}
1284
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001285
1286static char posix_execve__doc__[] =
1287"execve(path, args, env)\n\
1288Execute a path with arguments and environment, replacing current process.\n\
1289\n\
1290 path: path of executable file\n\
1291 args: tuple or list of arguments\n\
1292 env: dictonary of strings mapping to strings";
1293
Barry Warsaw53699e91996-12-10 23:23:01 +00001294static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001295posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001296 PyObject *self;
1297 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001298{
1299 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001300 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001301 char **argvlist;
1302 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001303 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001304 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001305 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001306
1307 /* execve has three arguments: (path, argv, env), where
1308 argv is a list or tuple of strings and env is a dictionary
1309 like posix.environ. */
1310
Barry Warsaw53699e91996-12-10 23:23:01 +00001311 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001312 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001313 if (PyList_Check(argv)) {
1314 argc = PyList_Size(argv);
1315 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001316 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001317 else if (PyTuple_Check(argv)) {
1318 argc = PyTuple_Size(argv);
1319 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001320 }
1321 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001322 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001323 return NULL;
1324 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001325 if (!PyMapping_Check(env)) {
1326 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001327 return NULL;
1328 }
1329
Barry Warsaw53699e91996-12-10 23:23:01 +00001330 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001331 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001332 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001333 return NULL;
1334 }
1335 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001336 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001337 "s;argv must be list of strings",
1338 &argvlist[i]))
1339 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001340 goto fail_1;
1341 }
1342 }
1343 argvlist[argc] = NULL;
1344
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001345 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001346 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001347 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001348 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001349 goto fail_1;
1350 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001351 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001352 keys = PyMapping_Keys(env);
1353 vals = PyMapping_Values(env);
1354 if (!keys || !vals)
1355 goto fail_2;
1356
1357 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001358 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001359
1360 key = PyList_GetItem(keys, pos);
1361 val = PyList_GetItem(vals, pos);
1362 if (!key || !val)
1363 goto fail_2;
1364
Barry Warsaw53699e91996-12-10 23:23:01 +00001365 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001366 !PyArg_Parse(val, "s;non-string value in env", &v))
1367 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001368 goto fail_2;
1369 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001370
1371#if defined(PYOS_OS2)
1372 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1373 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1374#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001375 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001376 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001377 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001378 goto fail_2;
1379 }
1380 sprintf(p, "%s=%s", k, v);
1381 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001382#if defined(PYOS_OS2)
1383 }
1384#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001385 }
1386 envlist[envc] = 0;
1387
Guido van Rossumb6775db1994-08-01 11:34:53 +00001388
1389#ifdef BAD_EXEC_PROTOTYPES
1390 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001391#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001392 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001393#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001394
1395 /* If we get here it's definitely an error */
1396
1397 (void) posix_error();
1398
1399 fail_2:
1400 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001401 PyMem_DEL(envlist[envc]);
1402 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001403 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001404 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001405 Py_XDECREF(vals);
1406 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001407 return NULL;
1408}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001409#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001410
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001411
Guido van Rossuma1065681999-01-25 23:20:23 +00001412#ifdef HAVE_SPAWNV
1413static char posix_spawnv__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001414"spawnv(mode, path, args)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001415Execute an executable path with arguments, replacing current process.\n\
1416\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001417 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001418 path: path of executable file\n\
1419 args: tuple or list of strings";
1420
1421static PyObject *
1422posix_spawnv(self, args)
1423 PyObject *self;
1424 PyObject *args;
1425{
1426 char *path;
1427 PyObject *argv;
1428 char **argvlist;
1429 int mode, i, argc;
1430 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1431
1432 /* spawnv has three arguments: (mode, path, argv), where
1433 argv is a list or tuple of strings. */
1434
1435 if (!PyArg_Parse(args, "(isO)", &mode, &path, &argv))
1436 return NULL;
1437 if (PyList_Check(argv)) {
1438 argc = PyList_Size(argv);
1439 getitem = PyList_GetItem;
1440 }
1441 else if (PyTuple_Check(argv)) {
1442 argc = PyTuple_Size(argv);
1443 getitem = PyTuple_GetItem;
1444 }
1445 else {
1446 badarg:
1447 PyErr_BadArgument();
1448 return NULL;
1449 }
1450
1451 argvlist = PyMem_NEW(char *, argc+1);
1452 if (argvlist == NULL)
1453 return NULL;
1454 for (i = 0; i < argc; i++) {
1455 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1456 PyMem_DEL(argvlist);
1457 goto badarg;
1458 }
1459 }
1460 argvlist[argc] = NULL;
1461
Guido van Rossum246bc171999-02-01 23:54:31 +00001462 if (mode == _OLD_P_OVERLAY)
1463 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001464 i = _spawnv(mode, path, argvlist);
1465
1466 PyMem_DEL(argvlist);
1467
1468 if (i == -1)
1469 return posix_error();
1470 else
1471 return Py_BuildValue("i", i);
1472}
1473
1474
1475static char posix_spawnve__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001476"spawnve(mode, path, args, env)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001477Execute a path with arguments and environment, replacing current process.\n\
1478\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001479 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001480 path: path of executable file\n\
1481 args: tuple or list of arguments\n\
1482 env: dictonary of strings mapping to strings";
1483
1484static PyObject *
1485posix_spawnve(self, args)
1486 PyObject *self;
1487 PyObject *args;
1488{
1489 char *path;
1490 PyObject *argv, *env;
1491 char **argvlist;
1492 char **envlist;
1493 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1494 int mode, i, pos, argc, envc;
1495 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1496
1497 /* spawnve has four arguments: (mode, path, argv, env), where
1498 argv is a list or tuple of strings and env is a dictionary
1499 like posix.environ. */
1500
1501 if (!PyArg_Parse(args, "(isOO)", &mode, &path, &argv, &env))
1502 return NULL;
1503 if (PyList_Check(argv)) {
1504 argc = PyList_Size(argv);
1505 getitem = PyList_GetItem;
1506 }
1507 else if (PyTuple_Check(argv)) {
1508 argc = PyTuple_Size(argv);
1509 getitem = PyTuple_GetItem;
1510 }
1511 else {
1512 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1513 return NULL;
1514 }
1515 if (!PyMapping_Check(env)) {
1516 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1517 return NULL;
1518 }
1519
1520 argvlist = PyMem_NEW(char *, argc+1);
1521 if (argvlist == NULL) {
1522 PyErr_NoMemory();
1523 return NULL;
1524 }
1525 for (i = 0; i < argc; i++) {
1526 if (!PyArg_Parse((*getitem)(argv, i),
1527 "s;argv must be list of strings",
1528 &argvlist[i]))
1529 {
1530 goto fail_1;
1531 }
1532 }
1533 argvlist[argc] = NULL;
1534
1535 i = PyMapping_Length(env);
1536 envlist = PyMem_NEW(char *, i + 1);
1537 if (envlist == NULL) {
1538 PyErr_NoMemory();
1539 goto fail_1;
1540 }
1541 envc = 0;
1542 keys = PyMapping_Keys(env);
1543 vals = PyMapping_Values(env);
1544 if (!keys || !vals)
1545 goto fail_2;
1546
1547 for (pos = 0; pos < i; pos++) {
1548 char *p, *k, *v;
1549
1550 key = PyList_GetItem(keys, pos);
1551 val = PyList_GetItem(vals, pos);
1552 if (!key || !val)
1553 goto fail_2;
1554
1555 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1556 !PyArg_Parse(val, "s;non-string value in env", &v))
1557 {
1558 goto fail_2;
1559 }
1560 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1561 if (p == NULL) {
1562 PyErr_NoMemory();
1563 goto fail_2;
1564 }
1565 sprintf(p, "%s=%s", k, v);
1566 envlist[envc++] = p;
1567 }
1568 envlist[envc] = 0;
1569
Guido van Rossum246bc171999-02-01 23:54:31 +00001570 if (mode == _OLD_P_OVERLAY)
1571 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001572 i = _spawnve(mode, path, argvlist, envlist);
1573 if (i == -1)
1574 (void) posix_error();
1575 else
1576 res = Py_BuildValue("i", i);
1577
1578 fail_2:
1579 while (--envc >= 0)
1580 PyMem_DEL(envlist[envc]);
1581 PyMem_DEL(envlist);
1582 fail_1:
1583 PyMem_DEL(argvlist);
1584 Py_XDECREF(vals);
1585 Py_XDECREF(keys);
1586 return res;
1587}
1588#endif /* HAVE_SPAWNV */
1589
1590
Guido van Rossumad0ee831995-03-01 10:34:45 +00001591#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001592static char posix_fork__doc__[] =
1593"fork() -> pid\n\
1594Fork a child process.\n\
1595\n\
1596Return 0 to child process and PID of child to parent process.";
1597
Barry Warsaw53699e91996-12-10 23:23:01 +00001598static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001599posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001600 PyObject *self;
1601 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001602{
1603 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001604 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001605 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001606 pid = fork();
1607 if (pid == -1)
1608 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001609 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001610 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001611}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001612#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001613
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001614
Guido van Rossumad0ee831995-03-01 10:34:45 +00001615#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001616static char posix_getegid__doc__[] =
1617"getegid() -> egid\n\
1618Return the current process's effective group id.";
1619
Barry Warsaw53699e91996-12-10 23:23:01 +00001620static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001621posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001622 PyObject *self;
1623 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001624{
Barry Warsaw53699e91996-12-10 23:23:01 +00001625 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001626 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001627 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001628}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001629#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001630
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001631
Guido van Rossumad0ee831995-03-01 10:34:45 +00001632#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001633static char posix_geteuid__doc__[] =
1634"geteuid() -> euid\n\
1635Return the current process's effective user id.";
1636
Barry Warsaw53699e91996-12-10 23:23:01 +00001637static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001638posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001639 PyObject *self;
1640 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001641{
Barry Warsaw53699e91996-12-10 23:23:01 +00001642 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001643 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001644 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001645}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001646#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001647
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001648
Guido van Rossumad0ee831995-03-01 10:34:45 +00001649#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001650static char posix_getgid__doc__[] =
1651"getgid() -> gid\n\
1652Return the current process's group id.";
1653
Barry Warsaw53699e91996-12-10 23:23:01 +00001654static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001655posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001656 PyObject *self;
1657 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001658{
Barry Warsaw53699e91996-12-10 23:23:01 +00001659 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001660 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001661 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001662}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001663#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001664
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001665
1666static char posix_getpid__doc__[] =
1667"getpid() -> pid\n\
1668Return the current process id";
1669
Barry Warsaw53699e91996-12-10 23:23:01 +00001670static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001671posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001672 PyObject *self;
1673 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001674{
Barry Warsaw53699e91996-12-10 23:23:01 +00001675 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001676 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001677 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001678}
1679
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001680
Guido van Rossumb6775db1994-08-01 11:34:53 +00001681#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001682static char posix_getpgrp__doc__[] =
1683"getpgrp() -> pgrp\n\
1684Return the current process group id.";
1685
Barry Warsaw53699e91996-12-10 23:23:01 +00001686static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001687posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001688 PyObject *self;
1689 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001690{
Barry Warsaw53699e91996-12-10 23:23:01 +00001691 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001692 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001693#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001694 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001695#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001696 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001697#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001698}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001699#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001700
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001701
Guido van Rossumb6775db1994-08-01 11:34:53 +00001702#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001703static char posix_setpgrp__doc__[] =
1704"setpgrp() -> None\n\
1705Make this process a session leader.";
1706
Barry Warsaw53699e91996-12-10 23:23:01 +00001707static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001708posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001709 PyObject *self;
1710 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001711{
Barry Warsaw53699e91996-12-10 23:23:01 +00001712 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001713 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001714#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001715 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001716#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001717 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001718#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001719 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001720 Py_INCREF(Py_None);
1721 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001722}
1723
Guido van Rossumb6775db1994-08-01 11:34:53 +00001724#endif /* HAVE_SETPGRP */
1725
Guido van Rossumad0ee831995-03-01 10:34:45 +00001726#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001727static char posix_getppid__doc__[] =
1728"getppid() -> ppid\n\
1729Return the parent's process id.";
1730
Barry Warsaw53699e91996-12-10 23:23:01 +00001731static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001732posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001733 PyObject *self;
1734 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001735{
Barry Warsaw53699e91996-12-10 23:23:01 +00001736 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001737 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001738 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001739}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001740#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001741
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001742
Guido van Rossumad0ee831995-03-01 10:34:45 +00001743#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001744static char posix_getuid__doc__[] =
1745"getuid() -> uid\n\
1746Return the current process's user id.";
1747
Barry Warsaw53699e91996-12-10 23:23:01 +00001748static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001749posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001750 PyObject *self;
1751 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001752{
Barry Warsaw53699e91996-12-10 23:23:01 +00001753 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001754 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001755 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001756}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001757#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001758
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001759
Guido van Rossumad0ee831995-03-01 10:34:45 +00001760#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001761static char posix_kill__doc__[] =
1762"kill(pid, sig) -> None\n\
1763Kill a process with a signal.";
1764
Barry Warsaw53699e91996-12-10 23:23:01 +00001765static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001766posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001767 PyObject *self;
1768 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001769{
1770 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001771 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001772 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001773#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001774 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1775 APIRET rc;
1776 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001777 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001778
1779 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1780 APIRET rc;
1781 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001782 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001783
1784 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001785 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001786#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001787 if (kill(pid, sig) == -1)
1788 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001789#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001790 Py_INCREF(Py_None);
1791 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001792}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001793#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001794
Guido van Rossumc0125471996-06-28 18:55:32 +00001795#ifdef HAVE_PLOCK
1796
1797#ifdef HAVE_SYS_LOCK_H
1798#include <sys/lock.h>
1799#endif
1800
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001801static char posix_plock__doc__[] =
1802"plock(op) -> None\n\
1803Lock program segments into memory.";
1804
Barry Warsaw53699e91996-12-10 23:23:01 +00001805static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001806posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001807 PyObject *self;
1808 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001809{
1810 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001811 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001812 return NULL;
1813 if (plock(op) == -1)
1814 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001815 Py_INCREF(Py_None);
1816 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001817}
1818#endif
1819
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001820
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001821#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001822static char posix_popen__doc__[] =
1823"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1824Open a pipe to/from a command returning a file object.";
1825
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001826#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001827static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001828async_system(const char *command)
1829{
1830 char *p, errormsg[256], args[1024];
1831 RESULTCODES rcodes;
1832 APIRET rc;
1833 char *shell = getenv("COMSPEC");
1834 if (!shell)
1835 shell = "cmd";
1836
1837 strcpy(args, shell);
1838 p = &args[ strlen(args)+1 ];
1839 strcpy(p, "/c ");
1840 strcat(p, command);
1841 p += strlen(p) + 1;
1842 *p = '\0';
1843
1844 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001845 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001846 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001847 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001848 &rcodes, shell);
1849 return rc;
1850}
1851
Guido van Rossumd48f2521997-12-05 22:19:34 +00001852static FILE *
1853popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001854{
1855 HFILE rhan, whan;
1856 FILE *retfd = NULL;
1857 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1858
Guido van Rossumd48f2521997-12-05 22:19:34 +00001859 if (rc != NO_ERROR) {
1860 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001861 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001862 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001863
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001864 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1865 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001866
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001867 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1868 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001869
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001870 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1871 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001872
1873 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001874 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001875 }
1876
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001877 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1878 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001879
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001880 close(oldfd); /* And Close Saved STDOUT Handle */
1881 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001882
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001883 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1884 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001885
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001886 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1887 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001888
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001889 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1890 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001891
1892 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001893 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001894 }
1895
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001896 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1897 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001898
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001899 close(oldfd); /* And Close Saved STDIN Handle */
1900 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001901
Guido van Rossumd48f2521997-12-05 22:19:34 +00001902 } else {
1903 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001904 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001905 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001906}
1907
1908static PyObject *
1909posix_popen(self, args)
1910 PyObject *self;
1911 PyObject *args;
1912{
1913 char *name;
1914 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001915 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001916 FILE *fp;
1917 PyObject *f;
1918 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1919 return NULL;
1920 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001921 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001922 Py_END_ALLOW_THREADS
1923 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001924 return os2_error(err);
1925
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001926 f = PyFile_FromFile(fp, name, mode, fclose);
1927 if (f != NULL)
1928 PyFile_SetBufSize(f, bufsize);
1929 return f;
1930}
1931
1932#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001933static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001934posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001935 PyObject *self;
1936 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001937{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001938 char *name;
1939 char *mode = "r";
1940 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001941 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001942 PyObject *f;
1943 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001944 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001945 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001946 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001947 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001948 if (fp == NULL)
1949 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001950 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001951 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001952 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001953 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001954}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001955#endif
1956
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001957#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001958
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001959
Guido van Rossumb6775db1994-08-01 11:34:53 +00001960#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001961static char posix_setuid__doc__[] =
1962"setuid(uid) -> None\n\
1963Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001964static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001965posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001966 PyObject *self;
1967 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001968{
1969 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001970 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001971 return NULL;
1972 if (setuid(uid) < 0)
1973 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001974 Py_INCREF(Py_None);
1975 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001976}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001977#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001978
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001979
Guido van Rossumb6775db1994-08-01 11:34:53 +00001980#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001981static char posix_setgid__doc__[] =
1982"setgid(gid) -> None\n\
1983Set the current process's group id.";
1984
Barry Warsaw53699e91996-12-10 23:23:01 +00001985static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001986posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001987 PyObject *self;
1988 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001989{
1990 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001991 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001992 return NULL;
1993 if (setgid(gid) < 0)
1994 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001995 Py_INCREF(Py_None);
1996 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001997}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001998#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001999
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002000
Guido van Rossumb6775db1994-08-01 11:34:53 +00002001#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002002static char posix_waitpid__doc__[] =
2003"waitpid(pid, options) -> (pid, status)\n\
2004Wait for completion of a give child process.";
2005
Barry Warsaw53699e91996-12-10 23:23:01 +00002006static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002007posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002008 PyObject *self;
2009 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002010{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002011 int pid, options;
2012#ifdef UNION_WAIT
2013 union wait status;
2014#define status_i (status.w_status)
2015#else
2016 int status;
2017#define status_i status
2018#endif
2019 status_i = 0;
2020
Barry Warsaw53699e91996-12-10 23:23:01 +00002021 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002022 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002023 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002024#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002025 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002026#else
2027 pid = waitpid(pid, &status, options);
2028#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002029 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002030 if (pid == -1)
2031 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002032 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002033 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002034}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002035#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002036
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002037
Guido van Rossumad0ee831995-03-01 10:34:45 +00002038#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002039static char posix_wait__doc__[] =
2040"wait() -> (pid, status)\n\
2041Wait for completion of a child process.";
2042
Barry Warsaw53699e91996-12-10 23:23:01 +00002043static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002044posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002045 PyObject *self;
2046 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00002047{
2048 int pid, sts;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002049#ifdef UNION_WAIT
2050 union wait status;
2051#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002052#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002053 int status;
2054#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002055#endif
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002056 status_i = 0;
2057 Py_BEGIN_ALLOW_THREADS
2058 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00002059 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00002060 if (pid == -1)
2061 return posix_error();
2062 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002063 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002064}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002065#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002066
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002067
2068static char posix_lstat__doc__[] =
2069"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
2070Like stat(path), but do not follow symbolic links.";
2071
Barry Warsaw53699e91996-12-10 23:23:01 +00002072static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002073posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002074 PyObject *self;
2075 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002076{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002077#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002078 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002079#else /* !HAVE_LSTAT */
2080 return posix_do_stat(self, args, stat);
2081#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002082}
2083
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002084
Guido van Rossumb6775db1994-08-01 11:34:53 +00002085#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002086static char posix_readlink__doc__[] =
2087"readlink(path) -> path\n\
2088Return a string representing the path to which the symbolic link points.";
2089
Barry Warsaw53699e91996-12-10 23:23:01 +00002090static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002091posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002092 PyObject *self;
2093 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002094{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002095 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002096 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002097 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002098 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002099 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002100 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00002101 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00002102 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002103 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002104 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002105 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002106}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002107#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002108
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002109
Guido van Rossumb6775db1994-08-01 11:34:53 +00002110#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002111static char posix_symlink__doc__[] =
2112"symlink(src, dst) -> None\n\
2113Create a symbolic link.";
2114
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002115static PyObject *
2116posix_symlink(self, args)
2117 PyObject *self;
2118 PyObject *args;
2119{
2120 return posix_2str(args, symlink);
2121}
2122#endif /* HAVE_SYMLINK */
2123
2124
2125#ifdef HAVE_TIMES
2126#ifndef HZ
2127#define HZ 60 /* Universal constant :-) */
2128#endif /* HZ */
2129
Guido van Rossumd48f2521997-12-05 22:19:34 +00002130#if defined(PYCC_VACPP) && defined(PYOS_OS2)
2131static long
2132system_uptime()
2133{
2134 ULONG value = 0;
2135
2136 Py_BEGIN_ALLOW_THREADS
2137 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
2138 Py_END_ALLOW_THREADS
2139
2140 return value;
2141}
2142
2143static PyObject *
2144posix_times(self, args)
2145 PyObject *self;
2146 PyObject *args;
2147{
2148 if (!PyArg_NoArgs(args))
2149 return NULL;
2150
2151 /* Currently Only Uptime is Provided -- Others Later */
2152 return Py_BuildValue("ddddd",
2153 (double)0 /* t.tms_utime / HZ */,
2154 (double)0 /* t.tms_stime / HZ */,
2155 (double)0 /* t.tms_cutime / HZ */,
2156 (double)0 /* t.tms_cstime / HZ */,
2157 (double)system_uptime() / 1000);
2158}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002159#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002160static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00002161posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002162 PyObject *self;
2163 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00002164{
2165 struct tms t;
2166 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00002167 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00002168 return NULL;
2169 errno = 0;
2170 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00002171 if (c == (clock_t) -1)
2172 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002173 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002174 (double)t.tms_utime / HZ,
2175 (double)t.tms_stime / HZ,
2176 (double)t.tms_cutime / HZ,
2177 (double)t.tms_cstime / HZ,
2178 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00002179}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002180#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002181#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002182
2183
Guido van Rossum87755a21996-09-07 00:59:43 +00002184#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002185#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00002186static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002187posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002188 PyObject *self;
2189 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002190{
2191 FILETIME create, exit, kernel, user;
2192 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00002193 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002194 return NULL;
2195 hProc = GetCurrentProcess();
2196 GetProcessTimes(hProc,&create, &exit, &kernel, &user);
Barry Warsaw53699e91996-12-10 23:23:01 +00002197 return Py_BuildValue(
2198 "ddddd",
2199 (double)(kernel.dwHighDateTime*2E32+kernel.dwLowDateTime)/2E6,
2200 (double)(user.dwHighDateTime*2E32+user.dwLowDateTime) / 2E6,
2201 (double)0,
2202 (double)0,
2203 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002204}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002205#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002206
2207#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00002208static char posix_times__doc__[] =
2209"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2210Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002211#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002212
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002213
Guido van Rossumb6775db1994-08-01 11:34:53 +00002214#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002215static char posix_setsid__doc__[] =
2216"setsid() -> None\n\
2217Call the system call setsid().";
2218
Barry Warsaw53699e91996-12-10 23:23:01 +00002219static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002220posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002221 PyObject *self;
2222 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002223{
Barry Warsaw53699e91996-12-10 23:23:01 +00002224 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002225 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002226 if (setsid() < 0)
2227 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002228 Py_INCREF(Py_None);
2229 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002230}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002231#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002232
Guido van Rossumb6775db1994-08-01 11:34:53 +00002233#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002234static char posix_setpgid__doc__[] =
2235"setpgid(pid, pgrp) -> None\n\
2236Call the system call setpgid().";
2237
Barry Warsaw53699e91996-12-10 23:23:01 +00002238static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002239posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002240 PyObject *self;
2241 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002242{
2243 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002244 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002245 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002246 if (setpgid(pid, pgrp) < 0)
2247 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002248 Py_INCREF(Py_None);
2249 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002250}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002251#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002252
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002253
Guido van Rossumb6775db1994-08-01 11:34:53 +00002254#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002255static char posix_tcgetpgrp__doc__[] =
2256"tcgetpgrp(fd) -> pgid\n\
2257Return the process group associated with the terminal given by a fd.";
2258
Barry Warsaw53699e91996-12-10 23:23:01 +00002259static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002260posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002261 PyObject *self;
2262 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002263{
2264 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002265 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002266 return NULL;
2267 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002268 if (pgid < 0)
2269 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002270 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002271}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002272#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002273
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002274
Guido van Rossumb6775db1994-08-01 11:34:53 +00002275#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002276static char posix_tcsetpgrp__doc__[] =
2277"tcsetpgrp(fd, pgid) -> None\n\
2278Set the process group associated with the terminal given by a fd.";
2279
Barry Warsaw53699e91996-12-10 23:23:01 +00002280static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002281posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002282 PyObject *self;
2283 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002284{
2285 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002286 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002287 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002288 if (tcsetpgrp(fd, pgid) < 0)
2289 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002290 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002291 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002292}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002293#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002294
Guido van Rossum687dd131993-05-17 08:34:16 +00002295/* Functions acting on file descriptors */
2296
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002297static char posix_open__doc__[] =
2298"open(filename, flag [, mode=0777]) -> fd\n\
2299Open a file (for low level IO).";
2300
Barry Warsaw53699e91996-12-10 23:23:01 +00002301static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002302posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002303 PyObject *self;
2304 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002305{
2306 char *file;
2307 int flag;
2308 int mode = 0777;
2309 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002310 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2311 return NULL;
2312
Barry Warsaw53699e91996-12-10 23:23:01 +00002313 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002314 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002315 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002316 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002317 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002318 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002319}
2320
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002321
2322static char posix_close__doc__[] =
2323"close(fd) -> None\n\
2324Close a file descriptor (for low level IO).";
2325
Barry Warsaw53699e91996-12-10 23:23:01 +00002326static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002327posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002328 PyObject *self;
2329 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002330{
2331 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002332 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002333 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002334 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002335 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002336 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002337 if (res < 0)
2338 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002339 Py_INCREF(Py_None);
2340 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002341}
2342
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002343
2344static char posix_dup__doc__[] =
2345"dup(fd) -> fd2\n\
2346Return a duplicate of a file descriptor.";
2347
Barry Warsaw53699e91996-12-10 23:23:01 +00002348static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002349posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002350 PyObject *self;
2351 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002352{
2353 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002354 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002355 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002356 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002357 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002358 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002359 if (fd < 0)
2360 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002361 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002362}
2363
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002364
2365static char posix_dup2__doc__[] =
2366"dup2(fd, fd2) -> None\n\
2367Duplicate file descriptor.";
2368
Barry Warsaw53699e91996-12-10 23:23:01 +00002369static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002370posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002371 PyObject *self;
2372 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002373{
2374 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002375 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002376 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002377 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002378 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002379 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002380 if (res < 0)
2381 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002382 Py_INCREF(Py_None);
2383 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002384}
2385
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002386
2387static char posix_lseek__doc__[] =
2388"lseek(fd, pos, how) -> newpos\n\
2389Set the current position of a file descriptor.";
2390
Barry Warsaw53699e91996-12-10 23:23:01 +00002391static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002392posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002393 PyObject *self;
2394 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002395{
2396 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002397 off_t pos, res;
2398 PyObject *posobj;
2399 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002400 return NULL;
2401#ifdef SEEK_SET
2402 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2403 switch (how) {
2404 case 0: how = SEEK_SET; break;
2405 case 1: how = SEEK_CUR; break;
2406 case 2: how = SEEK_END; break;
2407 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002408#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002409
2410#if !defined(HAVE_LARGEFILE_SUPPORT)
2411 pos = PyInt_AsLong(posobj);
2412#else
2413 pos = PyLong_Check(posobj) ?
2414 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2415#endif
2416 if (PyErr_Occurred())
2417 return NULL;
2418
Barry Warsaw53699e91996-12-10 23:23:01 +00002419 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002420 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002421 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002422 if (res < 0)
2423 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002424
2425#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002426 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002427#else
2428 return PyLong_FromLongLong(res);
2429#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002430}
2431
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002432
2433static char posix_read__doc__[] =
2434"read(fd, buffersize) -> string\n\
2435Read a file descriptor.";
2436
Barry Warsaw53699e91996-12-10 23:23:01 +00002437static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002438posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002439 PyObject *self;
2440 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002441{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002442 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002443 PyObject *buffer;
2444 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002445 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002446 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002447 if (buffer == NULL)
2448 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002449 Py_BEGIN_ALLOW_THREADS
2450 n = read(fd, PyString_AsString(buffer), size);
2451 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002452 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002453 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002454 return posix_error();
2455 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002456 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002457 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002458 return buffer;
2459}
2460
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002461
2462static char posix_write__doc__[] =
2463"write(fd, string) -> byteswritten\n\
2464Write a string to a file descriptor.";
2465
Barry Warsaw53699e91996-12-10 23:23:01 +00002466static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002467posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002468 PyObject *self;
2469 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002470{
2471 int fd, size;
2472 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002473 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002474 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002475 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002476 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002477 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002478 if (size < 0)
2479 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002480 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002481}
2482
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002483
2484static char posix_fstat__doc__[]=
2485"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2486Like stat(), but for an open file descriptor.";
2487
Barry Warsaw53699e91996-12-10 23:23:01 +00002488static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002489posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002490 PyObject *self;
2491 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002492{
2493 int fd;
2494 struct stat st;
2495 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002496 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002497 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002498 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002499 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002500 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002501 if (res != 0)
2502 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002503#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002504 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002505 (long)st.st_mode,
2506 (long)st.st_ino,
2507 (long)st.st_dev,
2508 (long)st.st_nlink,
2509 (long)st.st_uid,
2510 (long)st.st_gid,
2511 (long)st.st_size,
2512 (long)st.st_atime,
2513 (long)st.st_mtime,
2514 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002515#else
2516 return Py_BuildValue("(lLllllLlll)",
2517 (long)st.st_mode,
2518 (LONG_LONG)st.st_ino,
2519 (long)st.st_dev,
2520 (long)st.st_nlink,
2521 (long)st.st_uid,
2522 (long)st.st_gid,
2523 (LONG_LONG)st.st_size,
2524 (long)st.st_atime,
2525 (long)st.st_mtime,
2526 (long)st.st_ctime);
2527#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002528}
2529
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002530
2531static char posix_fdopen__doc__[] =
2532"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2533Return an open file object connected to a file descriptor.";
2534
Barry Warsaw53699e91996-12-10 23:23:01 +00002535static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002536posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002537 PyObject *self;
2538 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002539{
Barry Warsaw53699e91996-12-10 23:23:01 +00002540 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002541 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002542 char *mode = "r";
2543 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002544 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002545 PyObject *f;
2546 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002547 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002548
Barry Warsaw53699e91996-12-10 23:23:01 +00002549 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002550 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002551 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002552 if (fp == NULL)
2553 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002554 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002555 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002556 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002557 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002558}
2559
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002560
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002561#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002562static char posix_pipe__doc__[] =
2563"pipe() -> (read_end, write_end)\n\
2564Create a pipe.";
2565
Barry Warsaw53699e91996-12-10 23:23:01 +00002566static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002567posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002568 PyObject *self;
2569 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002570{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002571#if defined(PYOS_OS2)
2572 HFILE read, write;
2573 APIRET rc;
2574
2575 if (!PyArg_Parse(args, ""))
2576 return NULL;
2577
2578 Py_BEGIN_ALLOW_THREADS
2579 rc = DosCreatePipe( &read, &write, 4096);
2580 Py_END_ALLOW_THREADS
2581 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002582 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002583
2584 return Py_BuildValue("(ii)", read, write);
2585#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002586#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002587 int fds[2];
2588 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002589 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002590 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002591 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002592 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002593 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002594 if (res != 0)
2595 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002596 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002597#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002598 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002599 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002600 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002601 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002602 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002603 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002604 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002605 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002606 if (!ok)
2607 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002608 read_fd = _open_osfhandle((long)read, 0);
2609 write_fd = _open_osfhandle((long)write, 1);
2610 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002611#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002612#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002613}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002614#endif /* HAVE_PIPE */
2615
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002616
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002617#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002618static char posix_mkfifo__doc__[] =
2619"mkfifo(file, [, mode=0666]) -> None\n\
2620Create a FIFO (a POSIX named pipe).";
2621
Barry Warsaw53699e91996-12-10 23:23:01 +00002622static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002623posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002624 PyObject *self;
2625 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002626{
2627 char *file;
2628 int mode = 0666;
2629 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002630 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002631 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002632 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002633 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002634 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002635 if (res < 0)
2636 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002637 Py_INCREF(Py_None);
2638 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002639}
2640#endif
2641
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002642
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002643#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002644static char posix_ftruncate__doc__[] =
2645"ftruncate(fd, length) -> None\n\
2646Truncate a file to a specified length.";
2647
Barry Warsaw53699e91996-12-10 23:23:01 +00002648static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002649posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002650 PyObject *self; /* Not used */
2651 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002652{
2653 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002654 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002655 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002656 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002657
Guido van Rossum94f6f721999-01-06 18:42:14 +00002658 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2659 return NULL;
2660
2661#if !defined(HAVE_LARGEFILE_SUPPORT)
2662 length = PyInt_AsLong(lenobj);
2663#else
2664 length = PyLong_Check(lenobj) ?
2665 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2666#endif
2667 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002668 return NULL;
2669
Barry Warsaw53699e91996-12-10 23:23:01 +00002670 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002671 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002672 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002673 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002674 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002675 return NULL;
2676 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002677 Py_INCREF(Py_None);
2678 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002679}
2680#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002681
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002682#ifdef NeXT
2683#define HAVE_PUTENV
2684/* Steve Spicklemire got this putenv from NeXTAnswers */
2685static int
2686putenv(char *newval)
2687{
2688 extern char **environ;
2689
2690 static int firstTime = 1;
2691 char **ep;
2692 char *cp;
2693 int esiz;
2694 char *np;
2695
2696 if (!(np = strchr(newval, '=')))
2697 return 1;
2698 *np = '\0';
2699
2700 /* look it up */
2701 for (ep=environ ; *ep ; ep++)
2702 {
2703 /* this should always be true... */
2704 if (cp = strchr(*ep, '='))
2705 {
2706 *cp = '\0';
2707 if (!strcmp(*ep, newval))
2708 {
2709 /* got it! */
2710 *cp = '=';
2711 break;
2712 }
2713 *cp = '=';
2714 }
2715 else
2716 {
2717 *np = '=';
2718 return 1;
2719 }
2720 }
2721
2722 *np = '=';
2723 if (*ep)
2724 {
2725 /* the string was already there:
2726 just replace it with the new one */
2727 *ep = newval;
2728 return 0;
2729 }
2730
2731 /* expand environ by one */
2732 for (esiz=2, ep=environ ; *ep ; ep++)
2733 esiz++;
2734 if (firstTime)
2735 {
2736 char **epp;
2737 char **newenv;
2738 if (!(newenv = malloc(esiz * sizeof(char *))))
2739 return 1;
2740
2741 for (ep=environ, epp=newenv ; *ep ;)
2742 *epp++ = *ep++;
2743 *epp++ = newval;
2744 *epp = (char *) 0;
2745 environ = newenv;
2746 }
2747 else
2748 {
2749 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2750 return 1;
2751 environ[esiz - 2] = newval;
2752 environ[esiz - 1] = (char *) 0;
2753 firstTime = 0;
2754 }
2755
2756 return 0;
2757}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002758#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002759
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002760
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002761#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002762static char posix_putenv__doc__[] =
2763"putenv(key, value) -> None\n\
2764Change or add an environment variable.";
2765
Guido van Rossumbcc20741998-08-04 22:53:56 +00002766#ifdef __BEOS__
2767/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2768int putenv( const char *str );
2769#endif
2770
Barry Warsaw53699e91996-12-10 23:23:01 +00002771static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002772posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002773 PyObject *self;
2774 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002775{
2776 char *s1, *s2;
2777 char *new;
2778
Barry Warsaw53699e91996-12-10 23:23:01 +00002779 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002780 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002781
2782#if defined(PYOS_OS2)
2783 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2784 APIRET rc;
2785
2786 if (strlen(s2) == 0) /* If New Value is an Empty String */
2787 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2788
2789 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2790 if (rc != NO_ERROR)
2791 return os2_error(rc);
2792
2793 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2794 APIRET rc;
2795
2796 if (strlen(s2) == 0) /* If New Value is an Empty String */
2797 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2798
2799 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2800 if (rc != NO_ERROR)
2801 return os2_error(rc);
2802 } else {
2803#endif
2804
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002805 /* XXX This leaks memory -- not easy to fix :-( */
2806 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002807 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002808 (void) sprintf(new, "%s=%s", s1, s2);
2809 if (putenv(new)) {
2810 posix_error();
2811 return NULL;
2812 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002813
2814#if defined(PYOS_OS2)
2815 }
2816#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002817 Py_INCREF(Py_None);
2818 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002819}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002820#endif /* putenv */
2821
2822#ifdef HAVE_STRERROR
2823static char posix_strerror__doc__[] =
2824"strerror(code) -> string\n\
2825Translate an error code to a message string.";
2826
2827PyObject *
2828posix_strerror(self, args)
2829 PyObject *self;
2830 PyObject *args;
2831{
2832 int code;
2833 char *message;
2834 if (!PyArg_ParseTuple(args, "i", &code))
2835 return NULL;
2836 message = strerror(code);
2837 if (message == NULL) {
2838 PyErr_SetString(PyExc_ValueError,
2839 "strerror code out of range");
2840 return NULL;
2841 }
2842 return PyString_FromString(message);
2843}
2844#endif /* strerror */
2845
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002846
Guido van Rossumc9641791998-08-04 15:26:23 +00002847#ifdef HAVE_SYS_WAIT_H
2848
2849#ifdef WIFSTOPPED
2850static char posix_WIFSTOPPED__doc__[] =
2851"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002852Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002853
2854static PyObject *
2855posix_WIFSTOPPED(self, args)
2856 PyObject *self;
2857 PyObject *args;
2858{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002859#ifdef UNION_WAIT
2860 union wait status;
2861#define status_i (status.w_status)
2862#else
2863 int status;
2864#define status_i status
2865#endif
2866 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002867
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002868 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002869 {
2870 return NULL;
2871 }
2872
2873 return Py_BuildValue("i", WIFSTOPPED(status));
2874}
2875#endif /* WIFSTOPPED */
2876
2877#ifdef WIFSIGNALED
2878static char posix_WIFSIGNALED__doc__[] =
2879"WIFSIGNALED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002880Retrun true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002881
2882static PyObject *
2883posix_WIFSIGNALED(self, args)
2884 PyObject *self;
2885 PyObject *args;
2886{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002887#ifdef UNION_WAIT
2888 union wait status;
2889#define status_i (status.w_status)
2890#else
2891 int status;
2892#define status_i status
2893#endif
2894 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002895
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002896 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002897 {
2898 return NULL;
2899 }
2900
2901 return Py_BuildValue("i", WIFSIGNALED(status));
2902}
2903#endif /* WIFSIGNALED */
2904
2905#ifdef WIFEXITED
2906static char posix_WIFEXITED__doc__[] =
2907"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002908Return true if the process returning 'status' exited using the exit()\n\
2909system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002910
2911static PyObject *
2912posix_WIFEXITED(self, args)
2913 PyObject *self;
2914 PyObject *args;
2915{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002916#ifdef UNION_WAIT
2917 union wait status;
2918#define status_i (status.w_status)
2919#else
2920 int status;
2921#define status_i status
2922#endif
2923 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002924
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002925 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002926 {
2927 return NULL;
2928 }
2929
2930 return Py_BuildValue("i", WIFEXITED(status));
2931}
2932#endif /* WIFEXITED */
2933
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002934#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00002935static char posix_WEXITSTATUS__doc__[] =
2936"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002937Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002938
2939static PyObject *
2940posix_WEXITSTATUS(self, args)
2941 PyObject *self;
2942 PyObject *args;
2943{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002944#ifdef UNION_WAIT
2945 union wait status;
2946#define status_i (status.w_status)
2947#else
2948 int status;
2949#define status_i status
2950#endif
2951 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002952
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002953 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002954 {
2955 return NULL;
2956 }
2957
2958 return Py_BuildValue("i", WEXITSTATUS(status));
2959}
2960#endif /* WEXITSTATUS */
2961
2962#ifdef WTERMSIG
2963static char posix_WTERMSIG__doc__[] =
2964"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002965Return the signal that terminated the process that provided the 'status'\n\
2966value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002967
2968static PyObject *
2969posix_WTERMSIG(self, args)
2970 PyObject *self;
2971 PyObject *args;
2972{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002973#ifdef UNION_WAIT
2974 union wait status;
2975#define status_i (status.w_status)
2976#else
2977 int status;
2978#define status_i status
2979#endif
2980 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002981
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002982 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002983 {
2984 return NULL;
2985 }
2986
2987 return Py_BuildValue("i", WTERMSIG(status));
2988}
2989#endif /* WTERMSIG */
2990
2991#ifdef WSTOPSIG
2992static char posix_WSTOPSIG__doc__[] =
2993"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002994Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002995
2996static PyObject *
2997posix_WSTOPSIG(self, args)
2998 PyObject *self;
2999 PyObject *args;
3000{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003001#ifdef UNION_WAIT
3002 union wait status;
3003#define status_i (status.w_status)
3004#else
3005 int status;
3006#define status_i status
3007#endif
3008 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003009
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003010 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003011 {
3012 return NULL;
3013 }
3014
3015 return Py_BuildValue("i", WSTOPSIG(status));
3016}
3017#endif /* WSTOPSIG */
3018
3019#endif /* HAVE_SYS_WAIT_H */
3020
3021
Guido van Rossum94f6f721999-01-06 18:42:14 +00003022#if defined(HAVE_FSTATVFS)
3023#include <sys/statvfs.h>
3024
3025static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003026"fstatvfs(fd) -> \n\
3027 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003028Perform an fstatvfs system call on the given fd.";
3029
3030static PyObject *
3031posix_fstatvfs(self, args)
3032 PyObject *self;
3033 PyObject *args;
3034{
3035 int fd, res;
3036 struct statvfs st;
3037 if (!PyArg_ParseTuple(args, "i", &fd))
3038 return NULL;
3039 Py_BEGIN_ALLOW_THREADS
3040 res = fstatvfs(fd, &st);
3041 Py_END_ALLOW_THREADS
3042 if (res != 0)
3043 return posix_error();
3044#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003045 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003046 (long) st.f_bsize,
3047 (long) st.f_frsize,
3048 (long) st.f_blocks,
3049 (long) st.f_bfree,
3050 (long) st.f_bavail,
3051 (long) st.f_files,
3052 (long) st.f_ffree,
3053 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003054 (long) st.f_flag,
3055 (long) st.f_namemax);
3056#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003057 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003058 (long) st.f_bsize,
3059 (long) st.f_frsize,
3060 (LONG_LONG) st.f_blocks,
3061 (LONG_LONG) st.f_bfree,
3062 (LONG_LONG) st.f_bavail,
3063 (LONG_LONG) st.f_files,
3064 (LONG_LONG) st.f_ffree,
3065 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003066 (long) st.f_flag,
3067 (long) st.f_namemax);
3068#endif
3069}
3070#endif /* HAVE_FSTATVFS */
3071
3072
3073#if defined(HAVE_STATVFS)
3074#include <sys/statvfs.h>
3075
3076static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003077"statvfs(path) -> \n\
3078 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003079Perform a statvfs system call on the given path.";
3080
3081static PyObject *
3082posix_statvfs(self, args)
3083 PyObject *self;
3084 PyObject *args;
3085{
3086 char *path;
3087 int res;
3088 struct statvfs st;
3089 if (!PyArg_ParseTuple(args, "s", &path))
3090 return NULL;
3091 Py_BEGIN_ALLOW_THREADS
3092 res = statvfs(path, &st);
3093 Py_END_ALLOW_THREADS
3094 if (res != 0)
3095 return posix_error_with_filename(path);
3096#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003097 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003098 (long) st.f_bsize,
3099 (long) st.f_frsize,
3100 (long) st.f_blocks,
3101 (long) st.f_bfree,
3102 (long) st.f_bavail,
3103 (long) st.f_files,
3104 (long) st.f_ffree,
3105 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003106 (long) st.f_flag,
3107 (long) st.f_namemax);
3108#else /* HAVE_LARGEFILE_SUPPORT */
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003109 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003110 (long) st.f_bsize,
3111 (long) st.f_frsize,
3112 (LONG_LONG) st.f_blocks,
3113 (LONG_LONG) st.f_bfree,
3114 (LONG_LONG) st.f_bavail,
3115 (LONG_LONG) st.f_files,
3116 (LONG_LONG) st.f_ffree,
3117 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003118 (long) st.f_flag,
3119 (long) st.f_namemax);
3120#endif
3121}
3122#endif /* HAVE_STATVFS */
3123
3124
Barry Warsaw53699e91996-12-10 23:23:01 +00003125static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00003126 {"access", posix_access, 0, posix_access__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003127#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +00003128 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003129#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003130 {"chdir", posix_chdir, 0, posix_chdir__doc__},
3131 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003132#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003133 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003134#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00003135#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003136 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00003137#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00003138#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003139 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003140#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003141 {"listdir", posix_listdir, 0, posix_listdir__doc__},
3142 {"lstat", posix_lstat, 0, posix_lstat__doc__},
3143 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003144#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003145 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003146#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003147#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003148 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003149#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003150 {"rename", posix_rename, 0, posix_rename__doc__},
3151 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
3152 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003153#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003154 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003155#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003156#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003157 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003158#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003159 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003160#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003161 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003162#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003163 {"unlink", posix_unlink, 0, posix_unlink__doc__},
3164 {"remove", posix_unlink, 0, posix_remove__doc__},
3165 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003166#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003167 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003168#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003169 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003170#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003171 {"execv", posix_execv, 0, posix_execv__doc__},
3172 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003173#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00003174#ifdef HAVE_SPAWNV
3175 {"spawnv", posix_spawnv, 0, posix_spawnv__doc__},
3176 {"spawnve", posix_spawnve, 0, posix_spawnve__doc__},
3177#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003178#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003179 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003180#endif /* HAVE_FORK */
3181#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003182 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003183#endif /* HAVE_GETEGID */
3184#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003185 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003186#endif /* HAVE_GETEUID */
3187#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003188 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003189#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003190 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003191#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003192 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003193#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003194#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003195 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003196#endif /* HAVE_GETPPID */
3197#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003198 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003199#endif /* HAVE_GETUID */
3200#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003201 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003202#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00003203#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003204 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00003205#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003206#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003207 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003208#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003209#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003210 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003211#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003212#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003213 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003214#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003215#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003216 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003217#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003218#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003219 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003220#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003221#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003222 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003223#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003224#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003225 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003226#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003227#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003228 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003229#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003230#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003231 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003232#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003233#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003234 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003235#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003236 {"open", posix_open, 1, posix_open__doc__},
3237 {"close", posix_close, 0, posix_close__doc__},
3238 {"dup", posix_dup, 0, posix_dup__doc__},
3239 {"dup2", posix_dup2, 0, posix_dup2__doc__},
3240 {"lseek", posix_lseek, 0, posix_lseek__doc__},
3241 {"read", posix_read, 0, posix_read__doc__},
3242 {"write", posix_write, 0, posix_write__doc__},
3243 {"fstat", posix_fstat, 0, posix_fstat__doc__},
3244 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003245#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003246 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003247#endif
3248#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003249 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003250#endif
3251#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003252 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003253#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003254#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003255 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003256#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00003257#ifdef HAVE_STRERROR
3258 {"strerror", posix_strerror, 1, posix_strerror__doc__},
3259#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00003260#ifdef HAVE_FSYNC
3261 {"fsync", posix_fsync, 0, posix_fsync__doc__},
3262#endif
3263#ifdef HAVE_FDATASYNC
3264 {"fdatasync", posix_fdatasync, 0, posix_fdatasync__doc__},
3265#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00003266#ifdef HAVE_SYS_WAIT_H
3267#ifdef WIFSTOPPED
3268 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
3269#endif /* WIFSTOPPED */
3270#ifdef WIFSIGNALED
3271 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
3272#endif /* WIFSIGNALED */
3273#ifdef WIFEXITED
3274 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
3275#endif /* WIFEXITED */
3276#ifdef WEXITSTATUS
3277 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
3278#endif /* WEXITSTATUS */
3279#ifdef WTERMSIG
3280 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
3281#endif /* WTERMSIG */
3282#ifdef WSTOPSIG
3283 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
3284#endif /* WSTOPSIG */
3285#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003286#ifdef HAVE_FSTATVFS
3287 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
3288#endif
3289#ifdef HAVE_STATVFS
3290 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
3291#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003292 {NULL, NULL} /* Sentinel */
3293};
3294
3295
Barry Warsaw4a342091996-12-19 23:50:02 +00003296static int
3297ins(d, symbol, value)
3298 PyObject* d;
3299 char* symbol;
3300 long value;
3301{
3302 PyObject* v = PyInt_FromLong(value);
3303 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
3304 return -1; /* triggers fatal error */
3305
3306 Py_DECREF(v);
3307 return 0;
3308}
3309
Guido van Rossumd48f2521997-12-05 22:19:34 +00003310#if defined(PYOS_OS2)
3311/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
3312static int insertvalues(PyObject *d)
3313{
3314 APIRET rc;
3315 ULONG values[QSV_MAX+1];
3316 PyObject *v;
3317 char *ver, tmp[10];
3318
3319 Py_BEGIN_ALLOW_THREADS
3320 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
3321 Py_END_ALLOW_THREADS
3322
3323 if (rc != NO_ERROR) {
3324 os2_error(rc);
3325 return -1;
3326 }
3327
3328 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3329 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3330 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3331 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3332 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3333 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3334 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3335
3336 switch (values[QSV_VERSION_MINOR]) {
3337 case 0: ver = "2.00"; break;
3338 case 10: ver = "2.10"; break;
3339 case 11: ver = "2.11"; break;
3340 case 30: ver = "3.00"; break;
3341 case 40: ver = "4.00"; break;
3342 case 50: ver = "5.00"; break;
3343 default:
3344 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3345 values[QSV_VERSION_MINOR]);
3346 ver = &tmp[0];
3347 }
3348
3349 /* Add Indicator of the Version of the Operating System */
3350 v = PyString_FromString(ver);
3351 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3352 return -1;
3353 Py_DECREF(v);
3354
3355 /* Add Indicator of Which Drive was Used to Boot the System */
3356 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3357 tmp[1] = ':';
3358 tmp[2] = '\0';
3359
3360 v = PyString_FromString(tmp);
3361 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3362 return -1;
3363 Py_DECREF(v);
3364
3365 return 0;
3366}
3367#endif
3368
Barry Warsaw4a342091996-12-19 23:50:02 +00003369static int
3370all_ins(d)
3371 PyObject* d;
3372{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003373#ifdef F_OK
3374 if (ins(d, "F_OK", (long)F_OK)) return -1;
3375#endif
3376#ifdef R_OK
3377 if (ins(d, "R_OK", (long)R_OK)) return -1;
3378#endif
3379#ifdef W_OK
3380 if (ins(d, "W_OK", (long)W_OK)) return -1;
3381#endif
3382#ifdef X_OK
3383 if (ins(d, "X_OK", (long)X_OK)) return -1;
3384#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003385#ifdef WNOHANG
3386 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3387#endif
3388#ifdef O_RDONLY
3389 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3390#endif
3391#ifdef O_WRONLY
3392 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3393#endif
3394#ifdef O_RDWR
3395 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3396#endif
3397#ifdef O_NDELAY
3398 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3399#endif
3400#ifdef O_NONBLOCK
3401 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3402#endif
3403#ifdef O_APPEND
3404 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3405#endif
3406#ifdef O_DSYNC
3407 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3408#endif
3409#ifdef O_RSYNC
3410 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3411#endif
3412#ifdef O_SYNC
3413 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3414#endif
3415#ifdef O_NOCTTY
3416 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3417#endif
3418#ifdef O_CREAT
3419 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3420#endif
3421#ifdef O_EXCL
3422 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3423#endif
3424#ifdef O_TRUNC
3425 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3426#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003427#ifdef O_BINARY
3428 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3429#endif
3430#ifdef O_TEXT
3431 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3432#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003433
Guido van Rossum246bc171999-02-01 23:54:31 +00003434#ifdef HAVE_SPAWNV
3435 if (ins(d, "_P_WAIT", (long)_P_WAIT)) return -1;
3436 if (ins(d, "_P_NOWAIT", (long)_P_NOWAIT)) return -1;
3437 if (ins(d, "_P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
3438 if (ins(d, "_P_NOWAITO", (long)_P_NOWAITO)) return -1;
3439 if (ins(d, "_P_DETACH", (long)_P_DETACH)) return -1;
3440#endif
3441
Guido van Rossumd48f2521997-12-05 22:19:34 +00003442#if defined(PYOS_OS2)
3443 if (insertvalues(d)) return -1;
3444#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003445 return 0;
3446}
3447
3448
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003449#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003450#define INITFUNC initnt
3451#define MODNAME "nt"
3452#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003453#if defined(PYOS_OS2)
3454#define INITFUNC initos2
3455#define MODNAME "os2"
3456#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003457#define INITFUNC initposix
3458#define MODNAME "posix"
3459#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003460#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003461
Guido van Rossum3886bb61998-12-04 18:50:17 +00003462DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003463INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003464{
Barry Warsaw53699e91996-12-10 23:23:01 +00003465 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003466
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003467 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003468 posix_methods,
3469 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003470 (PyObject *)NULL,
3471 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003472 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003473
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003474 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003475 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003476 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003477 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003478 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003479
Barry Warsaw4a342091996-12-19 23:50:02 +00003480 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003481 return;
3482
Barry Warsawca74da41999-02-09 19:31:45 +00003483 PyDict_SetItemString(d, "error", PyExc_OSError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003484}