blob: 8dc079e618f6f08e26af7b61896a466441aabceb [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();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002196 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
2197 /* The fields of a FILETIME structure are the hi and lo part
2198 of a 64-bit value expressed in 100 nanosecond units.
2199 1e7 is one second in such units; 1e-7 the inverse.
2200 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
2201 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002202 return Py_BuildValue(
2203 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002204 (double)(kernel.dwHighDateTime*429.4967296 +
2205 kernel.dwLowDateTime*1e-7),
2206 (double)(user.dwHighDateTime*429.4967296 +
2207 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00002208 (double)0,
2209 (double)0,
2210 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002211}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002212#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002213
2214#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00002215static char posix_times__doc__[] =
2216"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2217Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002218#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002219
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002220
Guido van Rossumb6775db1994-08-01 11:34:53 +00002221#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002222static char posix_setsid__doc__[] =
2223"setsid() -> None\n\
2224Call the system call setsid().";
2225
Barry Warsaw53699e91996-12-10 23:23:01 +00002226static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002227posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002228 PyObject *self;
2229 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002230{
Barry Warsaw53699e91996-12-10 23:23:01 +00002231 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002232 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002233 if (setsid() < 0)
2234 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002235 Py_INCREF(Py_None);
2236 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002237}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002238#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002239
Guido van Rossumb6775db1994-08-01 11:34:53 +00002240#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002241static char posix_setpgid__doc__[] =
2242"setpgid(pid, pgrp) -> None\n\
2243Call the system call setpgid().";
2244
Barry Warsaw53699e91996-12-10 23:23:01 +00002245static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002246posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002247 PyObject *self;
2248 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002249{
2250 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002251 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002252 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002253 if (setpgid(pid, pgrp) < 0)
2254 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002255 Py_INCREF(Py_None);
2256 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002257}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002258#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002259
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002260
Guido van Rossumb6775db1994-08-01 11:34:53 +00002261#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002262static char posix_tcgetpgrp__doc__[] =
2263"tcgetpgrp(fd) -> pgid\n\
2264Return the process group associated with the terminal given by a fd.";
2265
Barry Warsaw53699e91996-12-10 23:23:01 +00002266static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002267posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002268 PyObject *self;
2269 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002270{
2271 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002272 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002273 return NULL;
2274 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002275 if (pgid < 0)
2276 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002277 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002278}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002279#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002280
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002281
Guido van Rossumb6775db1994-08-01 11:34:53 +00002282#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002283static char posix_tcsetpgrp__doc__[] =
2284"tcsetpgrp(fd, pgid) -> None\n\
2285Set the process group associated with the terminal given by a fd.";
2286
Barry Warsaw53699e91996-12-10 23:23:01 +00002287static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002288posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002289 PyObject *self;
2290 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002291{
2292 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002293 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002294 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002295 if (tcsetpgrp(fd, pgid) < 0)
2296 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002297 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002298 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002299}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002300#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002301
Guido van Rossum687dd131993-05-17 08:34:16 +00002302/* Functions acting on file descriptors */
2303
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002304static char posix_open__doc__[] =
2305"open(filename, flag [, mode=0777]) -> fd\n\
2306Open a file (for low level IO).";
2307
Barry Warsaw53699e91996-12-10 23:23:01 +00002308static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002309posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002310 PyObject *self;
2311 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002312{
2313 char *file;
2314 int flag;
2315 int mode = 0777;
2316 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002317 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2318 return NULL;
2319
Barry Warsaw53699e91996-12-10 23:23:01 +00002320 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002321 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002322 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002323 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002324 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002325 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002326}
2327
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002328
2329static char posix_close__doc__[] =
2330"close(fd) -> None\n\
2331Close a file descriptor (for low level IO).";
2332
Barry Warsaw53699e91996-12-10 23:23:01 +00002333static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002334posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002335 PyObject *self;
2336 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002337{
2338 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002339 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002340 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002341 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002342 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002343 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002344 if (res < 0)
2345 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002346 Py_INCREF(Py_None);
2347 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002348}
2349
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002350
2351static char posix_dup__doc__[] =
2352"dup(fd) -> fd2\n\
2353Return a duplicate of a file descriptor.";
2354
Barry Warsaw53699e91996-12-10 23:23:01 +00002355static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002356posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002357 PyObject *self;
2358 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002359{
2360 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002361 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002362 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002363 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002364 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002365 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002366 if (fd < 0)
2367 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002368 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002369}
2370
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002371
2372static char posix_dup2__doc__[] =
2373"dup2(fd, fd2) -> None\n\
2374Duplicate file descriptor.";
2375
Barry Warsaw53699e91996-12-10 23:23:01 +00002376static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002377posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002378 PyObject *self;
2379 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002380{
2381 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002382 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002383 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002384 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002385 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002386 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002387 if (res < 0)
2388 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002389 Py_INCREF(Py_None);
2390 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002391}
2392
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002393
2394static char posix_lseek__doc__[] =
2395"lseek(fd, pos, how) -> newpos\n\
2396Set the current position of a file descriptor.";
2397
Barry Warsaw53699e91996-12-10 23:23:01 +00002398static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002399posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002400 PyObject *self;
2401 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002402{
2403 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002404 off_t pos, res;
2405 PyObject *posobj;
2406 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002407 return NULL;
2408#ifdef SEEK_SET
2409 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2410 switch (how) {
2411 case 0: how = SEEK_SET; break;
2412 case 1: how = SEEK_CUR; break;
2413 case 2: how = SEEK_END; break;
2414 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002415#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002416
2417#if !defined(HAVE_LARGEFILE_SUPPORT)
2418 pos = PyInt_AsLong(posobj);
2419#else
2420 pos = PyLong_Check(posobj) ?
2421 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2422#endif
2423 if (PyErr_Occurred())
2424 return NULL;
2425
Barry Warsaw53699e91996-12-10 23:23:01 +00002426 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002427 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002428 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002429 if (res < 0)
2430 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002431
2432#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002433 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002434#else
2435 return PyLong_FromLongLong(res);
2436#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002437}
2438
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002439
2440static char posix_read__doc__[] =
2441"read(fd, buffersize) -> string\n\
2442Read a file descriptor.";
2443
Barry Warsaw53699e91996-12-10 23:23:01 +00002444static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002445posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002446 PyObject *self;
2447 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002448{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002449 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002450 PyObject *buffer;
2451 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002452 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002453 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002454 if (buffer == NULL)
2455 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002456 Py_BEGIN_ALLOW_THREADS
2457 n = read(fd, PyString_AsString(buffer), size);
2458 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002459 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002460 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002461 return posix_error();
2462 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002463 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002464 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002465 return buffer;
2466}
2467
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002468
2469static char posix_write__doc__[] =
2470"write(fd, string) -> byteswritten\n\
2471Write a string to a file descriptor.";
2472
Barry Warsaw53699e91996-12-10 23:23:01 +00002473static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002474posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002475 PyObject *self;
2476 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002477{
2478 int fd, size;
2479 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002480 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002481 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002482 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002483 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002484 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002485 if (size < 0)
2486 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002487 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002488}
2489
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002490
2491static char posix_fstat__doc__[]=
2492"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2493Like stat(), but for an open file descriptor.";
2494
Barry Warsaw53699e91996-12-10 23:23:01 +00002495static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002496posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002497 PyObject *self;
2498 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002499{
2500 int fd;
2501 struct stat st;
2502 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002503 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002504 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002505 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002506 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002507 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002508 if (res != 0)
2509 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002510#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002511 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002512 (long)st.st_mode,
2513 (long)st.st_ino,
2514 (long)st.st_dev,
2515 (long)st.st_nlink,
2516 (long)st.st_uid,
2517 (long)st.st_gid,
2518 (long)st.st_size,
2519 (long)st.st_atime,
2520 (long)st.st_mtime,
2521 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002522#else
2523 return Py_BuildValue("(lLllllLlll)",
2524 (long)st.st_mode,
2525 (LONG_LONG)st.st_ino,
2526 (long)st.st_dev,
2527 (long)st.st_nlink,
2528 (long)st.st_uid,
2529 (long)st.st_gid,
2530 (LONG_LONG)st.st_size,
2531 (long)st.st_atime,
2532 (long)st.st_mtime,
2533 (long)st.st_ctime);
2534#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002535}
2536
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002537
2538static char posix_fdopen__doc__[] =
2539"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2540Return an open file object connected to a file descriptor.";
2541
Barry Warsaw53699e91996-12-10 23:23:01 +00002542static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002543posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002544 PyObject *self;
2545 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002546{
Barry Warsaw53699e91996-12-10 23:23:01 +00002547 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002548 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002549 char *mode = "r";
2550 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002551 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002552 PyObject *f;
2553 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002554 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002555
Barry Warsaw53699e91996-12-10 23:23:01 +00002556 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002557 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002558 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002559 if (fp == NULL)
2560 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002561 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002562 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002563 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002564 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002565}
2566
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002567
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002568#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002569static char posix_pipe__doc__[] =
2570"pipe() -> (read_end, write_end)\n\
2571Create a pipe.";
2572
Barry Warsaw53699e91996-12-10 23:23:01 +00002573static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002574posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002575 PyObject *self;
2576 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002577{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002578#if defined(PYOS_OS2)
2579 HFILE read, write;
2580 APIRET rc;
2581
2582 if (!PyArg_Parse(args, ""))
2583 return NULL;
2584
2585 Py_BEGIN_ALLOW_THREADS
2586 rc = DosCreatePipe( &read, &write, 4096);
2587 Py_END_ALLOW_THREADS
2588 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002589 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002590
2591 return Py_BuildValue("(ii)", read, write);
2592#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002593#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002594 int fds[2];
2595 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002596 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002597 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002598 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002599 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002600 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002601 if (res != 0)
2602 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002603 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002604#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002605 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002606 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002607 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002608 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002609 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002610 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002611 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002612 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002613 if (!ok)
2614 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002615 read_fd = _open_osfhandle((long)read, 0);
2616 write_fd = _open_osfhandle((long)write, 1);
2617 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002618#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002619#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002620}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002621#endif /* HAVE_PIPE */
2622
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002623
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002624#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002625static char posix_mkfifo__doc__[] =
2626"mkfifo(file, [, mode=0666]) -> None\n\
2627Create a FIFO (a POSIX named pipe).";
2628
Barry Warsaw53699e91996-12-10 23:23:01 +00002629static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002630posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002631 PyObject *self;
2632 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002633{
2634 char *file;
2635 int mode = 0666;
2636 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002637 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002638 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002639 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002640 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002641 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002642 if (res < 0)
2643 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002644 Py_INCREF(Py_None);
2645 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002646}
2647#endif
2648
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002649
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002650#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002651static char posix_ftruncate__doc__[] =
2652"ftruncate(fd, length) -> None\n\
2653Truncate a file to a specified length.";
2654
Barry Warsaw53699e91996-12-10 23:23:01 +00002655static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002656posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002657 PyObject *self; /* Not used */
2658 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002659{
2660 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002661 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002662 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002663 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002664
Guido van Rossum94f6f721999-01-06 18:42:14 +00002665 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2666 return NULL;
2667
2668#if !defined(HAVE_LARGEFILE_SUPPORT)
2669 length = PyInt_AsLong(lenobj);
2670#else
2671 length = PyLong_Check(lenobj) ?
2672 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2673#endif
2674 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002675 return NULL;
2676
Barry Warsaw53699e91996-12-10 23:23:01 +00002677 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002678 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002679 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002680 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002681 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002682 return NULL;
2683 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002684 Py_INCREF(Py_None);
2685 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002686}
2687#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002688
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002689#ifdef NeXT
2690#define HAVE_PUTENV
2691/* Steve Spicklemire got this putenv from NeXTAnswers */
2692static int
2693putenv(char *newval)
2694{
2695 extern char **environ;
2696
2697 static int firstTime = 1;
2698 char **ep;
2699 char *cp;
2700 int esiz;
2701 char *np;
2702
2703 if (!(np = strchr(newval, '=')))
2704 return 1;
2705 *np = '\0';
2706
2707 /* look it up */
2708 for (ep=environ ; *ep ; ep++)
2709 {
2710 /* this should always be true... */
2711 if (cp = strchr(*ep, '='))
2712 {
2713 *cp = '\0';
2714 if (!strcmp(*ep, newval))
2715 {
2716 /* got it! */
2717 *cp = '=';
2718 break;
2719 }
2720 *cp = '=';
2721 }
2722 else
2723 {
2724 *np = '=';
2725 return 1;
2726 }
2727 }
2728
2729 *np = '=';
2730 if (*ep)
2731 {
2732 /* the string was already there:
2733 just replace it with the new one */
2734 *ep = newval;
2735 return 0;
2736 }
2737
2738 /* expand environ by one */
2739 for (esiz=2, ep=environ ; *ep ; ep++)
2740 esiz++;
2741 if (firstTime)
2742 {
2743 char **epp;
2744 char **newenv;
2745 if (!(newenv = malloc(esiz * sizeof(char *))))
2746 return 1;
2747
2748 for (ep=environ, epp=newenv ; *ep ;)
2749 *epp++ = *ep++;
2750 *epp++ = newval;
2751 *epp = (char *) 0;
2752 environ = newenv;
2753 }
2754 else
2755 {
2756 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2757 return 1;
2758 environ[esiz - 2] = newval;
2759 environ[esiz - 1] = (char *) 0;
2760 firstTime = 0;
2761 }
2762
2763 return 0;
2764}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002765#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002766
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002767
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002768#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002769static char posix_putenv__doc__[] =
2770"putenv(key, value) -> None\n\
2771Change or add an environment variable.";
2772
Guido van Rossumbcc20741998-08-04 22:53:56 +00002773#ifdef __BEOS__
2774/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2775int putenv( const char *str );
2776#endif
2777
Barry Warsaw53699e91996-12-10 23:23:01 +00002778static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002779posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002780 PyObject *self;
2781 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002782{
2783 char *s1, *s2;
2784 char *new;
2785
Barry Warsaw53699e91996-12-10 23:23:01 +00002786 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002787 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002788
2789#if defined(PYOS_OS2)
2790 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2791 APIRET rc;
2792
2793 if (strlen(s2) == 0) /* If New Value is an Empty String */
2794 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2795
2796 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2797 if (rc != NO_ERROR)
2798 return os2_error(rc);
2799
2800 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2801 APIRET rc;
2802
2803 if (strlen(s2) == 0) /* If New Value is an Empty String */
2804 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2805
2806 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2807 if (rc != NO_ERROR)
2808 return os2_error(rc);
2809 } else {
2810#endif
2811
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002812 /* XXX This leaks memory -- not easy to fix :-( */
2813 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002814 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002815 (void) sprintf(new, "%s=%s", s1, s2);
2816 if (putenv(new)) {
2817 posix_error();
2818 return NULL;
2819 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002820
2821#if defined(PYOS_OS2)
2822 }
2823#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002824 Py_INCREF(Py_None);
2825 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002826}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002827#endif /* putenv */
2828
2829#ifdef HAVE_STRERROR
2830static char posix_strerror__doc__[] =
2831"strerror(code) -> string\n\
2832Translate an error code to a message string.";
2833
2834PyObject *
2835posix_strerror(self, args)
2836 PyObject *self;
2837 PyObject *args;
2838{
2839 int code;
2840 char *message;
2841 if (!PyArg_ParseTuple(args, "i", &code))
2842 return NULL;
2843 message = strerror(code);
2844 if (message == NULL) {
2845 PyErr_SetString(PyExc_ValueError,
2846 "strerror code out of range");
2847 return NULL;
2848 }
2849 return PyString_FromString(message);
2850}
2851#endif /* strerror */
2852
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002853
Guido van Rossumc9641791998-08-04 15:26:23 +00002854#ifdef HAVE_SYS_WAIT_H
2855
2856#ifdef WIFSTOPPED
2857static char posix_WIFSTOPPED__doc__[] =
2858"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002859Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002860
2861static PyObject *
2862posix_WIFSTOPPED(self, args)
2863 PyObject *self;
2864 PyObject *args;
2865{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002866#ifdef UNION_WAIT
2867 union wait status;
2868#define status_i (status.w_status)
2869#else
2870 int status;
2871#define status_i status
2872#endif
2873 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002874
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002875 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002876 {
2877 return NULL;
2878 }
2879
2880 return Py_BuildValue("i", WIFSTOPPED(status));
2881}
2882#endif /* WIFSTOPPED */
2883
2884#ifdef WIFSIGNALED
2885static char posix_WIFSIGNALED__doc__[] =
2886"WIFSIGNALED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002887Retrun true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002888
2889static PyObject *
2890posix_WIFSIGNALED(self, args)
2891 PyObject *self;
2892 PyObject *args;
2893{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002894#ifdef UNION_WAIT
2895 union wait status;
2896#define status_i (status.w_status)
2897#else
2898 int status;
2899#define status_i status
2900#endif
2901 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002902
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002903 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002904 {
2905 return NULL;
2906 }
2907
2908 return Py_BuildValue("i", WIFSIGNALED(status));
2909}
2910#endif /* WIFSIGNALED */
2911
2912#ifdef WIFEXITED
2913static char posix_WIFEXITED__doc__[] =
2914"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002915Return true if the process returning 'status' exited using the exit()\n\
2916system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002917
2918static PyObject *
2919posix_WIFEXITED(self, args)
2920 PyObject *self;
2921 PyObject *args;
2922{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002923#ifdef UNION_WAIT
2924 union wait status;
2925#define status_i (status.w_status)
2926#else
2927 int status;
2928#define status_i status
2929#endif
2930 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002931
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002932 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002933 {
2934 return NULL;
2935 }
2936
2937 return Py_BuildValue("i", WIFEXITED(status));
2938}
2939#endif /* WIFEXITED */
2940
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002941#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00002942static char posix_WEXITSTATUS__doc__[] =
2943"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002944Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002945
2946static PyObject *
2947posix_WEXITSTATUS(self, args)
2948 PyObject *self;
2949 PyObject *args;
2950{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002951#ifdef UNION_WAIT
2952 union wait status;
2953#define status_i (status.w_status)
2954#else
2955 int status;
2956#define status_i status
2957#endif
2958 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002959
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002960 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002961 {
2962 return NULL;
2963 }
2964
2965 return Py_BuildValue("i", WEXITSTATUS(status));
2966}
2967#endif /* WEXITSTATUS */
2968
2969#ifdef WTERMSIG
2970static char posix_WTERMSIG__doc__[] =
2971"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002972Return the signal that terminated the process that provided the 'status'\n\
2973value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002974
2975static PyObject *
2976posix_WTERMSIG(self, args)
2977 PyObject *self;
2978 PyObject *args;
2979{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002980#ifdef UNION_WAIT
2981 union wait status;
2982#define status_i (status.w_status)
2983#else
2984 int status;
2985#define status_i status
2986#endif
2987 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002988
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002989 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002990 {
2991 return NULL;
2992 }
2993
2994 return Py_BuildValue("i", WTERMSIG(status));
2995}
2996#endif /* WTERMSIG */
2997
2998#ifdef WSTOPSIG
2999static char posix_WSTOPSIG__doc__[] =
3000"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003001Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003002
3003static PyObject *
3004posix_WSTOPSIG(self, args)
3005 PyObject *self;
3006 PyObject *args;
3007{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003008#ifdef UNION_WAIT
3009 union wait status;
3010#define status_i (status.w_status)
3011#else
3012 int status;
3013#define status_i status
3014#endif
3015 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003016
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003017 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003018 {
3019 return NULL;
3020 }
3021
3022 return Py_BuildValue("i", WSTOPSIG(status));
3023}
3024#endif /* WSTOPSIG */
3025
3026#endif /* HAVE_SYS_WAIT_H */
3027
3028
Guido van Rossum94f6f721999-01-06 18:42:14 +00003029#if defined(HAVE_FSTATVFS)
3030#include <sys/statvfs.h>
3031
3032static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003033"fstatvfs(fd) -> \n\
3034 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003035Perform an fstatvfs system call on the given fd.";
3036
3037static PyObject *
3038posix_fstatvfs(self, args)
3039 PyObject *self;
3040 PyObject *args;
3041{
3042 int fd, res;
3043 struct statvfs st;
3044 if (!PyArg_ParseTuple(args, "i", &fd))
3045 return NULL;
3046 Py_BEGIN_ALLOW_THREADS
3047 res = fstatvfs(fd, &st);
3048 Py_END_ALLOW_THREADS
3049 if (res != 0)
3050 return posix_error();
3051#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003052 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003053 (long) st.f_bsize,
3054 (long) st.f_frsize,
3055 (long) st.f_blocks,
3056 (long) st.f_bfree,
3057 (long) st.f_bavail,
3058 (long) st.f_files,
3059 (long) st.f_ffree,
3060 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003061 (long) st.f_flag,
3062 (long) st.f_namemax);
3063#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003064 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003065 (long) st.f_bsize,
3066 (long) st.f_frsize,
3067 (LONG_LONG) st.f_blocks,
3068 (LONG_LONG) st.f_bfree,
3069 (LONG_LONG) st.f_bavail,
3070 (LONG_LONG) st.f_files,
3071 (LONG_LONG) st.f_ffree,
3072 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003073 (long) st.f_flag,
3074 (long) st.f_namemax);
3075#endif
3076}
3077#endif /* HAVE_FSTATVFS */
3078
3079
3080#if defined(HAVE_STATVFS)
3081#include <sys/statvfs.h>
3082
3083static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003084"statvfs(path) -> \n\
3085 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003086Perform a statvfs system call on the given path.";
3087
3088static PyObject *
3089posix_statvfs(self, args)
3090 PyObject *self;
3091 PyObject *args;
3092{
3093 char *path;
3094 int res;
3095 struct statvfs st;
3096 if (!PyArg_ParseTuple(args, "s", &path))
3097 return NULL;
3098 Py_BEGIN_ALLOW_THREADS
3099 res = statvfs(path, &st);
3100 Py_END_ALLOW_THREADS
3101 if (res != 0)
3102 return posix_error_with_filename(path);
3103#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003104 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003105 (long) st.f_bsize,
3106 (long) st.f_frsize,
3107 (long) st.f_blocks,
3108 (long) st.f_bfree,
3109 (long) st.f_bavail,
3110 (long) st.f_files,
3111 (long) st.f_ffree,
3112 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003113 (long) st.f_flag,
3114 (long) st.f_namemax);
3115#else /* HAVE_LARGEFILE_SUPPORT */
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003116 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003117 (long) st.f_bsize,
3118 (long) st.f_frsize,
3119 (LONG_LONG) st.f_blocks,
3120 (LONG_LONG) st.f_bfree,
3121 (LONG_LONG) st.f_bavail,
3122 (LONG_LONG) st.f_files,
3123 (LONG_LONG) st.f_ffree,
3124 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003125 (long) st.f_flag,
3126 (long) st.f_namemax);
3127#endif
3128}
3129#endif /* HAVE_STATVFS */
3130
3131
Barry Warsaw53699e91996-12-10 23:23:01 +00003132static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00003133 {"access", posix_access, 0, posix_access__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003134#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +00003135 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003136#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003137 {"chdir", posix_chdir, 0, posix_chdir__doc__},
3138 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003139#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003140 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003141#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00003142#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003143 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00003144#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00003145#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003146 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003147#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003148 {"listdir", posix_listdir, 0, posix_listdir__doc__},
3149 {"lstat", posix_lstat, 0, posix_lstat__doc__},
3150 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003151#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003152 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003153#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003154#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003155 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003156#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003157 {"rename", posix_rename, 0, posix_rename__doc__},
3158 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
3159 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003160#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003161 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003162#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003163#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003164 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003165#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003166 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003167#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003168 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003169#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003170 {"unlink", posix_unlink, 0, posix_unlink__doc__},
3171 {"remove", posix_unlink, 0, posix_remove__doc__},
3172 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003173#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003174 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003175#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003176 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003177#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003178 {"execv", posix_execv, 0, posix_execv__doc__},
3179 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003180#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00003181#ifdef HAVE_SPAWNV
3182 {"spawnv", posix_spawnv, 0, posix_spawnv__doc__},
3183 {"spawnve", posix_spawnve, 0, posix_spawnve__doc__},
3184#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003185#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003186 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003187#endif /* HAVE_FORK */
3188#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003189 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003190#endif /* HAVE_GETEGID */
3191#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003192 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003193#endif /* HAVE_GETEUID */
3194#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003195 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003196#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003197 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003198#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003199 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003200#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003201#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003202 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003203#endif /* HAVE_GETPPID */
3204#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003205 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003206#endif /* HAVE_GETUID */
3207#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003208 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003209#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00003210#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003211 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00003212#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003213#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003214 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003215#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003216#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003217 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003218#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003219#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003220 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003221#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003222#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003223 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003224#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003225#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003226 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003227#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003228#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003229 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003230#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003231#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003232 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003233#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003234#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003235 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003236#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003237#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003238 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003239#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003240#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003241 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003242#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003243 {"open", posix_open, 1, posix_open__doc__},
3244 {"close", posix_close, 0, posix_close__doc__},
3245 {"dup", posix_dup, 0, posix_dup__doc__},
3246 {"dup2", posix_dup2, 0, posix_dup2__doc__},
3247 {"lseek", posix_lseek, 0, posix_lseek__doc__},
3248 {"read", posix_read, 0, posix_read__doc__},
3249 {"write", posix_write, 0, posix_write__doc__},
3250 {"fstat", posix_fstat, 0, posix_fstat__doc__},
3251 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003252#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003253 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003254#endif
3255#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003256 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003257#endif
3258#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003259 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003260#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003261#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003262 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003263#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00003264#ifdef HAVE_STRERROR
3265 {"strerror", posix_strerror, 1, posix_strerror__doc__},
3266#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00003267#ifdef HAVE_FSYNC
3268 {"fsync", posix_fsync, 0, posix_fsync__doc__},
3269#endif
3270#ifdef HAVE_FDATASYNC
3271 {"fdatasync", posix_fdatasync, 0, posix_fdatasync__doc__},
3272#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00003273#ifdef HAVE_SYS_WAIT_H
3274#ifdef WIFSTOPPED
3275 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
3276#endif /* WIFSTOPPED */
3277#ifdef WIFSIGNALED
3278 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
3279#endif /* WIFSIGNALED */
3280#ifdef WIFEXITED
3281 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
3282#endif /* WIFEXITED */
3283#ifdef WEXITSTATUS
3284 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
3285#endif /* WEXITSTATUS */
3286#ifdef WTERMSIG
3287 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
3288#endif /* WTERMSIG */
3289#ifdef WSTOPSIG
3290 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
3291#endif /* WSTOPSIG */
3292#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003293#ifdef HAVE_FSTATVFS
3294 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
3295#endif
3296#ifdef HAVE_STATVFS
3297 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
3298#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003299 {NULL, NULL} /* Sentinel */
3300};
3301
3302
Barry Warsaw4a342091996-12-19 23:50:02 +00003303static int
3304ins(d, symbol, value)
3305 PyObject* d;
3306 char* symbol;
3307 long value;
3308{
3309 PyObject* v = PyInt_FromLong(value);
3310 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
3311 return -1; /* triggers fatal error */
3312
3313 Py_DECREF(v);
3314 return 0;
3315}
3316
Guido van Rossumd48f2521997-12-05 22:19:34 +00003317#if defined(PYOS_OS2)
3318/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
3319static int insertvalues(PyObject *d)
3320{
3321 APIRET rc;
3322 ULONG values[QSV_MAX+1];
3323 PyObject *v;
3324 char *ver, tmp[10];
3325
3326 Py_BEGIN_ALLOW_THREADS
3327 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
3328 Py_END_ALLOW_THREADS
3329
3330 if (rc != NO_ERROR) {
3331 os2_error(rc);
3332 return -1;
3333 }
3334
3335 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3336 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3337 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3338 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3339 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3340 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3341 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3342
3343 switch (values[QSV_VERSION_MINOR]) {
3344 case 0: ver = "2.00"; break;
3345 case 10: ver = "2.10"; break;
3346 case 11: ver = "2.11"; break;
3347 case 30: ver = "3.00"; break;
3348 case 40: ver = "4.00"; break;
3349 case 50: ver = "5.00"; break;
3350 default:
3351 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3352 values[QSV_VERSION_MINOR]);
3353 ver = &tmp[0];
3354 }
3355
3356 /* Add Indicator of the Version of the Operating System */
3357 v = PyString_FromString(ver);
3358 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3359 return -1;
3360 Py_DECREF(v);
3361
3362 /* Add Indicator of Which Drive was Used to Boot the System */
3363 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3364 tmp[1] = ':';
3365 tmp[2] = '\0';
3366
3367 v = PyString_FromString(tmp);
3368 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3369 return -1;
3370 Py_DECREF(v);
3371
3372 return 0;
3373}
3374#endif
3375
Barry Warsaw4a342091996-12-19 23:50:02 +00003376static int
3377all_ins(d)
3378 PyObject* d;
3379{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003380#ifdef F_OK
3381 if (ins(d, "F_OK", (long)F_OK)) return -1;
3382#endif
3383#ifdef R_OK
3384 if (ins(d, "R_OK", (long)R_OK)) return -1;
3385#endif
3386#ifdef W_OK
3387 if (ins(d, "W_OK", (long)W_OK)) return -1;
3388#endif
3389#ifdef X_OK
3390 if (ins(d, "X_OK", (long)X_OK)) return -1;
3391#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003392#ifdef WNOHANG
3393 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3394#endif
3395#ifdef O_RDONLY
3396 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3397#endif
3398#ifdef O_WRONLY
3399 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3400#endif
3401#ifdef O_RDWR
3402 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3403#endif
3404#ifdef O_NDELAY
3405 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3406#endif
3407#ifdef O_NONBLOCK
3408 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3409#endif
3410#ifdef O_APPEND
3411 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3412#endif
3413#ifdef O_DSYNC
3414 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3415#endif
3416#ifdef O_RSYNC
3417 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3418#endif
3419#ifdef O_SYNC
3420 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3421#endif
3422#ifdef O_NOCTTY
3423 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3424#endif
3425#ifdef O_CREAT
3426 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3427#endif
3428#ifdef O_EXCL
3429 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3430#endif
3431#ifdef O_TRUNC
3432 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3433#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003434#ifdef O_BINARY
3435 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3436#endif
3437#ifdef O_TEXT
3438 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3439#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003440
Guido van Rossum246bc171999-02-01 23:54:31 +00003441#ifdef HAVE_SPAWNV
3442 if (ins(d, "_P_WAIT", (long)_P_WAIT)) return -1;
3443 if (ins(d, "_P_NOWAIT", (long)_P_NOWAIT)) return -1;
3444 if (ins(d, "_P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
3445 if (ins(d, "_P_NOWAITO", (long)_P_NOWAITO)) return -1;
3446 if (ins(d, "_P_DETACH", (long)_P_DETACH)) return -1;
3447#endif
3448
Guido van Rossumd48f2521997-12-05 22:19:34 +00003449#if defined(PYOS_OS2)
3450 if (insertvalues(d)) return -1;
3451#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003452 return 0;
3453}
3454
3455
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003456#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003457#define INITFUNC initnt
3458#define MODNAME "nt"
3459#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003460#if defined(PYOS_OS2)
3461#define INITFUNC initos2
3462#define MODNAME "os2"
3463#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003464#define INITFUNC initposix
3465#define MODNAME "posix"
3466#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003467#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003468
Guido van Rossum3886bb61998-12-04 18:50:17 +00003469DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003470INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003471{
Barry Warsaw53699e91996-12-10 23:23:01 +00003472 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003473
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003474 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003475 posix_methods,
3476 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003477 (PyObject *)NULL,
3478 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003479 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003480
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003481 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003482 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003483 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003484 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003485 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003486
Barry Warsaw4a342091996-12-19 23:50:02 +00003487 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003488 return;
3489
Barry Warsawca74da41999-02-09 19:31:45 +00003490 PyDict_SetItemString(d, "error", PyExc_OSError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003491}