blob: 947e1691eb6a66034a27482a2dc36efce81aec0b [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 Rossum8c67e4e1999-04-07 15:49:41 +0000158extern int fsync();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000159#else /* !HAVE_UNISTD_H */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000160#if defined(PYCC_VACPP)
161extern int mkdir Py_PROTO((char *));
162#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000163#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Barry Warsaw53699e91996-12-10 23:23:01 +0000164extern int mkdir Py_PROTO((const char *));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000165#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000166extern int mkdir Py_PROTO((const char *, mode_t));
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000167#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000168#endif
169#if defined(__IBMC__) || defined(__IBMCPP__)
170extern int chdir Py_PROTO((char *));
171extern int rmdir Py_PROTO((char *));
172#else
Barry Warsaw53699e91996-12-10 23:23:01 +0000173extern int chdir Py_PROTO((const char *));
174extern int rmdir Py_PROTO((const char *));
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000175#endif
Barry Warsaw53699e91996-12-10 23:23:01 +0000176extern int chmod Py_PROTO((const char *, mode_t));
177extern int chown Py_PROTO((const char *, uid_t, gid_t));
178extern char *getcwd Py_PROTO((char *, int));
179extern char *strerror Py_PROTO((int));
180extern int link Py_PROTO((const char *, const char *));
181extern int rename Py_PROTO((const char *, const char *));
182extern int stat Py_PROTO((const char *, struct stat *));
183extern int unlink Py_PROTO((const char *));
184extern int pclose Py_PROTO((FILE *));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185#ifdef HAVE_SYMLINK
Barry Warsaw53699e91996-12-10 23:23:01 +0000186extern int symlink Py_PROTO((const char *, const char *));
Guido van Rossuma38a5031995-02-17 15:11:36 +0000187#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188#ifdef HAVE_LSTAT
Barry Warsaw53699e91996-12-10 23:23:01 +0000189extern int lstat Py_PROTO((const char *, struct stat *));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000190#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000191#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000192
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000193#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194
Guido van Rossumb6775db1994-08-01 11:34:53 +0000195#ifdef HAVE_UTIME_H
196#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000197#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000199#ifdef HAVE_SYS_UTIME_H
200#include <sys/utime.h>
201#define HAVE_UTIME_H /* pretend we do for the rest of this file */
202#endif /* HAVE_SYS_UTIME_H */
203
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204#ifdef HAVE_SYS_TIMES_H
205#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000206#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000207
208#ifdef HAVE_SYS_PARAM_H
209#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000210#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000211
212#ifdef HAVE_SYS_UTSNAME_H
213#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000214#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215
216#ifndef MAXPATHLEN
217#define MAXPATHLEN 1024
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000218#endif /* MAXPATHLEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000219
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000220#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000221#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000222#define NAMLEN(dirent) strlen((dirent)->d_name)
223#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000224#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000225#include <direct.h>
226#define NAMLEN(dirent) strlen((dirent)->d_name)
227#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000229#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000230#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000231#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000233#endif
234#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000236#endif
237#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000239#endif
240#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000241
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000242#ifdef _MSC_VER
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243#include <direct.h>
244#include <io.h>
245#include <process.h>
246#include <windows.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000247#ifdef MS_WIN32
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000248#define popen _popen
Guido van Rossum794d8131994-08-23 13:48:48 +0000249#define pclose _pclose
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000250#else /* 16-bit Windows */
251#include <dos.h>
252#include <ctype.h>
Guido van Rossum8d665e61996-06-26 18:22:49 +0000253#endif /* MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000254#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000255
Guido van Rossumd48f2521997-12-05 22:19:34 +0000256#if defined(PYCC_VACPP) && defined(PYOS_OS2)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#include <io.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000258#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000259
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000260#ifdef UNION_WAIT
261/* Emulate some macros on systems that have a union instead of macros */
262
263#ifndef WIFEXITED
264#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
265#endif
266
267#ifndef WEXITSTATUS
268#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
269#endif
270
271#ifndef WTERMSIG
272#define WTERMSIG(u_wait) ((u_wait).w_termsig)
273#endif
274
275#endif /* UNION_WAIT */
276
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277/* Return a dictionary corresponding to the POSIX environment table */
278
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000279#if !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000280extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000281#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000282
Barry Warsaw53699e91996-12-10 23:23:01 +0000283static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000284convertenviron()
285{
Barry Warsaw53699e91996-12-10 23:23:01 +0000286 PyObject *d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000287 char **e;
Barry Warsaw53699e91996-12-10 23:23:01 +0000288 d = PyDict_New();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289 if (d == NULL)
290 return NULL;
291 if (environ == NULL)
292 return d;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000293 /* This part ignores errors */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000294 for (e = environ; *e != NULL; e++) {
Guido van Rossum6a619f41999-08-03 19:41:10 +0000295 PyObject *k;
Barry Warsaw53699e91996-12-10 23:23:01 +0000296 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000297 char *p = strchr(*e, '=');
298 if (p == NULL)
299 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000300 k = PyString_FromStringAndSize(*e, (int)(p-*e));
301 if (k == NULL) {
302 PyErr_Clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000303 continue;
Guido van Rossum6a619f41999-08-03 19:41:10 +0000304 }
305 v = PyString_FromString(p+1);
306 if (v == NULL) {
307 PyErr_Clear();
308 Py_DECREF(k);
309 continue;
310 }
311 if (PyDict_GetItem(d, k) == NULL) {
312 if (PyDict_SetItem(d, k, v) != 0)
313 PyErr_Clear();
314 }
315 Py_DECREF(k);
Barry Warsaw53699e91996-12-10 23:23:01 +0000316 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000318#if defined(PYOS_OS2)
319 {
320 APIRET rc;
321 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
322
323 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
324 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
325 PyObject *v = PyString_FromString(buffer);
326 PyDict_SetItemString(d, "BEGINLIBPATH", v);
327 Py_DECREF(v);
328 }
329 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
330 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
331 PyObject *v = PyString_FromString(buffer);
332 PyDict_SetItemString(d, "ENDLIBPATH", v);
333 Py_DECREF(v);
334 }
335 }
336#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337 return d;
338}
339
340
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341/* Set a POSIX-specific error from errno, and return NULL */
342
Barry Warsawd58d7641998-07-23 16:14:40 +0000343static PyObject *
344posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000345{
Barry Warsawca74da41999-02-09 19:31:45 +0000346 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000347}
Barry Warsawd58d7641998-07-23 16:14:40 +0000348static PyObject *
349posix_error_with_filename(name)
350 char* name;
351{
Barry Warsawca74da41999-02-09 19:31:45 +0000352 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000353}
354
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355
Guido van Rossumd48f2521997-12-05 22:19:34 +0000356#if defined(PYOS_OS2)
357/**********************************************************************
358 * Helper Function to Trim and Format OS/2 Messages
359 **********************************************************************/
360 static void
361os2_formatmsg(char *msgbuf, int msglen, char *reason)
362{
363 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
364
365 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
366 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
367
368 while (lastc > msgbuf && isspace(*lastc))
369 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
370 }
371
372 /* Add Optional Reason Text */
373 if (reason) {
374 strcat(msgbuf, " : ");
375 strcat(msgbuf, reason);
376 }
377}
378
379/**********************************************************************
380 * Decode an OS/2 Operating System Error Code
381 *
382 * A convenience function to lookup an OS/2 error code and return a
383 * text message we can use to raise a Python exception.
384 *
385 * Notes:
386 * The messages for errors returned from the OS/2 kernel reside in
387 * the file OSO001.MSG in the \OS2 directory hierarchy.
388 *
389 **********************************************************************/
390 static char *
391os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
392{
393 APIRET rc;
394 ULONG msglen;
395
396 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
397 Py_BEGIN_ALLOW_THREADS
398 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
399 errorcode, "oso001.msg", &msglen);
400 Py_END_ALLOW_THREADS
401
402 if (rc == NO_ERROR)
403 os2_formatmsg(msgbuf, msglen, reason);
404 else
405 sprintf(msgbuf, "unknown OS error #%d", errorcode);
406
407 return msgbuf;
408}
409
410/* Set an OS/2-specific error and return NULL. OS/2 kernel
411 errors are not in a global variable e.g. 'errno' nor are
412 they congruent with posix error numbers. */
413
414static PyObject * os2_error(int code)
415{
416 char text[1024];
417 PyObject *v;
418
419 os2_strerror(text, sizeof(text), code, "");
420
421 v = Py_BuildValue("(is)", code, text);
422 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000423 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000424 Py_DECREF(v);
425 }
426 return NULL; /* Signal to Python that an Exception is Pending */
427}
428
429#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430
431/* POSIX generic methods */
432
Barry Warsaw53699e91996-12-10 23:23:01 +0000433static PyObject *
Guido van Rossum21142a01999-01-08 21:05:37 +0000434posix_int(args, func)
435 PyObject *args;
436 int (*func) Py_FPROTO((int));
437{
438 int fd;
439 int res;
440 if (!PyArg_Parse(args, "i", &fd))
441 return NULL;
442 Py_BEGIN_ALLOW_THREADS
443 res = (*func)(fd);
444 Py_END_ALLOW_THREADS
445 if (res < 0)
446 return posix_error();
447 Py_INCREF(Py_None);
448 return Py_None;
449}
450
451
452static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000454 PyObject *args;
455 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000457 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000458 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000459 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000460 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000461 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000462 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000463 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000464 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000465 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000466 Py_INCREF(Py_None);
467 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468}
469
Barry Warsaw53699e91996-12-10 23:23:01 +0000470static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000472 PyObject *args;
473 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000475 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000476 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000477 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000478 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000479 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000480 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000481 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000482 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000483 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000485 Py_INCREF(Py_None);
486 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487}
488
Barry Warsaw53699e91996-12-10 23:23:01 +0000489static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000491 PyObject *args;
492 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000493{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000494 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000495 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000496 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000497 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000499 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000500 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000501 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000502 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000503 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000504 Py_INCREF(Py_None);
505 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000506}
507
Barry Warsaw53699e91996-12-10 23:23:01 +0000508static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000509posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000510 PyObject *args;
511 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000512{
513 char *path;
514 int i,i2;
515 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000516 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000517 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000518 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000519 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000520 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000521 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000522 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000523 Py_INCREF(Py_None);
524 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000525}
526
Barry Warsaw53699e91996-12-10 23:23:01 +0000527static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000529 PyObject *self;
530 PyObject *args;
531 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000532{
533 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000534 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000535 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000536 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000537 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000538 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000539 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000540 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000541 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000542 return posix_error_with_filename(path);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000543#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000544 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000545 (long)st.st_mode,
546 (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)st.st_size,
552 (long)st.st_atime,
553 (long)st.st_mtime,
554 (long)st.st_ctime);
555#else
556 return Py_BuildValue("(lLllllLlll)",
557 (long)st.st_mode,
558 (LONG_LONG)st.st_ino,
559 (long)st.st_dev,
560 (long)st.st_nlink,
561 (long)st.st_uid,
562 (long)st.st_gid,
563 (LONG_LONG)st.st_size,
564 (long)st.st_atime,
565 (long)st.st_mtime,
566 (long)st.st_ctime);
567#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568}
569
570
571/* POSIX methods */
572
Guido van Rossum94f6f721999-01-06 18:42:14 +0000573static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000574"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000575Test for access to a file.";
576
577static PyObject *
578posix_access(self, args)
579 PyObject *self;
580 PyObject *args;
581{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000582 char *path;
583 int mode;
584 int res;
585
586 if (!PyArg_Parse(args, "(si)", &path, &mode))
587 return NULL;
588 Py_BEGIN_ALLOW_THREADS
589 res = access(path, mode);
590 Py_END_ALLOW_THREADS
591 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000592}
593
Guido van Rossumd371ff11999-01-25 16:12:23 +0000594#ifndef F_OK
595#define F_OK 0
596#endif
597#ifndef R_OK
598#define R_OK 4
599#endif
600#ifndef W_OK
601#define W_OK 2
602#endif
603#ifndef X_OK
604#define X_OK 1
605#endif
606
607#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000608static char posix_ttyname__doc__[] =
Guido van Rossum61eeb041999-02-22 15:29:15 +0000609"ttyname(fd) -> String\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000610Return the name of the terminal device connected to 'fd'.";
611
612static PyObject *
613posix_ttyname(self, args)
614 PyObject *self;
615 PyObject *args;
616{
617 PyObject *file;
618 int id;
619 char *ret;
620
Guido van Rossum94f6f721999-01-06 18:42:14 +0000621 if (!PyArg_Parse(args, "i", &id))
622 return NULL;
623
Guido van Rossum94f6f721999-01-06 18:42:14 +0000624 ret = ttyname(id);
625 if (ret == NULL)
626 return(posix_error());
627 return(PyString_FromString(ret));
628}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000629#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000630
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000631static char posix_chdir__doc__[] =
632"chdir(path) -> None\n\
633Change the current working directory to the specified path.";
634
Barry Warsaw53699e91996-12-10 23:23:01 +0000635static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000636posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000637 PyObject *self;
638 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000640 return posix_1str(args, chdir);
641}
642
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000643
644static char posix_chmod__doc__[] =
645"chmod(path, mode) -> None\n\
646Change the access permissions of a file.";
647
Barry Warsaw53699e91996-12-10 23:23:01 +0000648static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000650 PyObject *self;
651 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000652{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000653 return posix_strint(args, chmod);
654}
655
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000656
Guido van Rossum21142a01999-01-08 21:05:37 +0000657#ifdef HAVE_FSYNC
658static char posix_fsync__doc__[] =
659"fsync(fildes) -> None\n\
660force write of file with filedescriptor to disk.";
661
662static PyObject *
663posix_fsync(self, args)
664 PyObject *self;
665 PyObject *args;
666{
Guido van Rossum21142a01999-01-08 21:05:37 +0000667 return posix_int(args, fsync);
668}
669#endif /* HAVE_FSYNC */
670
671#ifdef HAVE_FDATASYNC
672static char posix_fdatasync__doc__[] =
673"fdatasync(fildes) -> None\n\
674force write of file with filedescriptor to disk.\n\
675 does not force update of metadata.";
676
Guido van Rossum5d00b6d1999-01-08 21:28:05 +0000677extern int fdatasync(int); /* Prototype just in case */
678
Guido van Rossum21142a01999-01-08 21:05:37 +0000679static PyObject *
680posix_fdatasync(self, args)
681 PyObject *self;
682 PyObject *args;
683{
Guido van Rossum21142a01999-01-08 21:05:37 +0000684 return posix_int(args, fdatasync);
685}
686#endif /* HAVE_FDATASYNC */
687
688
Guido van Rossumb6775db1994-08-01 11:34:53 +0000689#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000690static char posix_chown__doc__[] =
691"chown(path, uid, gid) -> None\n\
692Change the owner and group id of path to the numeric uid and gid.";
693
Barry Warsaw53699e91996-12-10 23:23:01 +0000694static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000695posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000696 PyObject *self;
697 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000698{
699 return posix_strintint(args, chown);
700}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000701#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000702
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000703
Guido van Rossum36bc6801995-06-14 22:54:23 +0000704#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000705static char posix_getcwd__doc__[] =
706"getcwd() -> path\n\
707Return a string representing the current working directory.";
708
Barry Warsaw53699e91996-12-10 23:23:01 +0000709static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000710posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000711 PyObject *self;
712 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000713{
714 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000715 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000716 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000717 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000718 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000719 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000720 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000721 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000722 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000723 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000724}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000725#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000726
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000727
Guido van Rossumb6775db1994-08-01 11:34:53 +0000728#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000729static char posix_link__doc__[] =
730"link(src, dst) -> None\n\
731Create a hard link to a file.";
732
Barry Warsaw53699e91996-12-10 23:23:01 +0000733static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000734posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000735 PyObject *self;
736 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000737{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000738 return posix_2str(args, link);
739}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000740#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000741
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000742
743static char posix_listdir__doc__[] =
744"listdir(path) -> list_of_strings\n\
745Return a list containing the names of the entries in the directory.\n\
746\n\
747 path: path of directory to list\n\
748\n\
749The list is in arbitrary order. It does not include the special\n\
750entries '.' and '..' even if they are present in the directory.";
751
Barry Warsaw53699e91996-12-10 23:23:01 +0000752static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000753posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000754 PyObject *self;
755 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000756{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000757 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000758 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000759#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000760
Guido van Rossumb6775db1994-08-01 11:34:53 +0000761 char *name;
762 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000763 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000764 HANDLE hFindFile;
765 WIN32_FIND_DATA FileData;
766 char namebuf[MAX_PATH+5];
767
Guido van Rossum7e488981998-10-08 02:25:24 +0000768 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000769 return NULL;
770 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000771 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000772 return NULL;
773 }
774 strcpy(namebuf, name);
775 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
776 namebuf[len++] = '/';
777 strcpy(namebuf + len, "*.*");
778
Barry Warsaw53699e91996-12-10 23:23:01 +0000779 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000780 return NULL;
781
782 hFindFile = FindFirstFile(namebuf, &FileData);
783 if (hFindFile == INVALID_HANDLE_VALUE) {
784 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000785 if (errno == ERROR_FILE_NOT_FOUND)
786 return PyList_New(0);
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000787 return posix_error_with_filename(name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788 }
789 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000790 if (FileData.cFileName[0] == '.' &&
791 (FileData.cFileName[1] == '\0' ||
792 FileData.cFileName[1] == '.' &&
793 FileData.cFileName[2] == '\0'))
794 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000795 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000796 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000797 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798 d = NULL;
799 break;
800 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000801 if (PyList_Append(d, v) != 0) {
802 Py_DECREF(v);
803 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000804 d = NULL;
805 break;
806 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000807 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000808 } while (FindNextFile(hFindFile, &FileData) == TRUE);
809
810 if (FindClose(hFindFile) == FALSE) {
811 errno = GetLastError();
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000812 return posix_error_with_filename(&name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000813 }
814
815 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000816
Guido van Rossum8d665e61996-06-26 18:22:49 +0000817#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000818#ifdef _MSC_VER /* 16-bit Windows */
819
820#ifndef MAX_PATH
821#define MAX_PATH 250
822#endif
823 char *name, *pt;
824 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000825 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000826 char namebuf[MAX_PATH+5];
827 struct _find_t ep;
828
Guido van Rossum7e488981998-10-08 02:25:24 +0000829 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000830 return NULL;
831 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000832 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000833 return NULL;
834 }
835 strcpy(namebuf, name);
836 for (pt = namebuf; *pt; pt++)
837 if (*pt == '/')
838 *pt = '\\';
839 if (namebuf[len-1] != '\\')
840 namebuf[len++] = '\\';
841 strcpy(namebuf + len, "*.*");
842
Barry Warsaw53699e91996-12-10 23:23:01 +0000843 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000844 return NULL;
845
846 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000847 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
848 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000849 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000850 return posix_error_with_filename(name);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000851 }
852 do {
853 if (ep.name[0] == '.' &&
854 (ep.name[1] == '\0' ||
855 ep.name[1] == '.' &&
856 ep.name[2] == '\0'))
857 continue;
858 strcpy(namebuf, ep.name);
859 for (pt = namebuf; *pt; pt++)
860 if (isupper(*pt))
861 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000862 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000863 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000864 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000865 d = NULL;
866 break;
867 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000868 if (PyList_Append(d, v) != 0) {
869 Py_DECREF(v);
870 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000871 d = NULL;
872 break;
873 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000874 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000875 } while (_dos_findnext(&ep) == 0);
876
877 return d;
878
879#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000880#if defined(PYOS_OS2)
881
882#ifndef MAX_PATH
883#define MAX_PATH CCHMAXPATH
884#endif
885 char *name, *pt;
886 int len;
887 PyObject *d, *v;
888 char namebuf[MAX_PATH+5];
889 HDIR hdir = 1;
890 ULONG srchcnt = 1;
891 FILEFINDBUF3 ep;
892 APIRET rc;
893
Guido van Rossum7e488981998-10-08 02:25:24 +0000894 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000895 return NULL;
896 if (len >= MAX_PATH) {
897 PyErr_SetString(PyExc_ValueError, "path too long");
898 return NULL;
899 }
900 strcpy(namebuf, name);
901 for (pt = namebuf; *pt; pt++)
902 if (*pt == '/')
903 *pt = '\\';
904 if (namebuf[len-1] != '\\')
905 namebuf[len++] = '\\';
906 strcpy(namebuf + len, "*.*");
907
908 if ((d = PyList_New(0)) == NULL)
909 return NULL;
910
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000911 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
912 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000913 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000914 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
915 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
916 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000917
918 if (rc != NO_ERROR) {
919 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000920 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000921 }
922
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000923 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000924 do {
925 if (ep.achName[0] == '.'
926 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000927 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000928
929 strcpy(namebuf, ep.achName);
930
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000931 /* Leave Case of Name Alone -- In Native Form */
932 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000933
934 v = PyString_FromString(namebuf);
935 if (v == NULL) {
936 Py_DECREF(d);
937 d = NULL;
938 break;
939 }
940 if (PyList_Append(d, v) != 0) {
941 Py_DECREF(v);
942 Py_DECREF(d);
943 d = NULL;
944 break;
945 }
946 Py_DECREF(v);
947 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
948 }
949
950 return d;
951#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000952
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000953 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000954 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000955 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000956 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000957 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000958 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000959 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000960 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000961 Py_BLOCK_THREADS
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000962 return posix_error_with_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000963 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000964 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000966 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967 return NULL;
968 }
969 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000970 if (ep->d_name[0] == '.' &&
971 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000972 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000973 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000974 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000975 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000976 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000977 d = NULL;
978 break;
979 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000980 if (PyList_Append(d, v) != 0) {
981 Py_DECREF(v);
982 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000983 d = NULL;
984 break;
985 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000986 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000987 }
988 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000989 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000990
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000991 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000992
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000993#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000994#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000995#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000996}
997
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000998static char posix_mkdir__doc__[] =
999"mkdir(path [, mode=0777]) -> None\n\
1000Create a directory.";
1001
Barry Warsaw53699e91996-12-10 23:23:01 +00001002static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001003posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001004 PyObject *self;
1005 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001006{
Guido van Rossumb0824db1996-02-25 04:50:32 +00001007 int res;
1008 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001009 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +00001010 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001011 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001012 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001013#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001014 res = mkdir(path);
1015#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001016 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001017#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001018 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001019 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001020 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001021 Py_INCREF(Py_None);
1022 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001023}
1024
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001025
Guido van Rossumb6775db1994-08-01 11:34:53 +00001026#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001027static char posix_nice__doc__[] =
1028"nice(inc) -> new_priority\n\
1029Decrease the priority of process and return new priority.";
1030
Barry Warsaw53699e91996-12-10 23:23:01 +00001031static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +00001032posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001033 PyObject *self;
1034 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001035{
1036 int increment, value;
1037
Barry Warsaw53699e91996-12-10 23:23:01 +00001038 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001039 return NULL;
1040 value = nice(increment);
1041 if (value == -1)
1042 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001043 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001044}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001045#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001046
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001047
1048static char posix_rename__doc__[] =
1049"rename(old, new) -> None\n\
1050Rename a file or directory.";
1051
Barry Warsaw53699e91996-12-10 23:23:01 +00001052static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001053posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001054 PyObject *self;
1055 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001056{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001057 return posix_2str(args, rename);
1058}
1059
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001060
1061static char posix_rmdir__doc__[] =
1062"rmdir(path) -> None\n\
1063Remove a directory.";
1064
Barry Warsaw53699e91996-12-10 23:23:01 +00001065static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001066posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001067 PyObject *self;
1068 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001069{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001070 return posix_1str(args, rmdir);
1071}
1072
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001073
1074static char posix_stat__doc__[] =
1075"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1076Perform a stat system call on the given path.";
1077
Barry Warsaw53699e91996-12-10 23:23:01 +00001078static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001079posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001080 PyObject *self;
1081 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001082{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001083 return posix_do_stat(self, args, stat);
1084}
1085
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001086
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001087#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001088static char posix_system__doc__[] =
1089"system(command) -> exit_status\n\
1090Execute the command (a string) in a subshell.";
1091
Barry Warsaw53699e91996-12-10 23:23:01 +00001092static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001093posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001094 PyObject *self;
1095 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001096{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001097 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001098 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001099 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001100 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001101 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001102 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001103 Py_END_ALLOW_THREADS
1104 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001105}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001106#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001107
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001108
1109static char posix_umask__doc__[] =
1110"umask(new_mask) -> old_mask\n\
1111Set the current numeric umask and return the previous umask.";
1112
Barry Warsaw53699e91996-12-10 23:23:01 +00001113static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001114posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001115 PyObject *self;
1116 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001117{
1118 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +00001119 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001120 return NULL;
1121 i = umask(i);
1122 if (i < 0)
1123 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001124 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001125}
1126
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001127
1128static char posix_unlink__doc__[] =
1129"unlink(path) -> None\n\
1130Remove a file (same as remove(path)).";
1131
1132static char posix_remove__doc__[] =
1133"remove(path) -> None\n\
1134Remove a file (same as unlink(path)).";
1135
Barry Warsaw53699e91996-12-10 23:23:01 +00001136static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001137posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001138 PyObject *self;
1139 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001140{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001141 return posix_1str(args, unlink);
1142}
1143
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001144
Guido van Rossumb6775db1994-08-01 11:34:53 +00001145#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001146static char posix_uname__doc__[] =
1147"uname() -> (sysname, nodename, release, version, machine)\n\
1148Return a tuple identifying the current operating system.";
1149
Barry Warsaw53699e91996-12-10 23:23:01 +00001150static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001151posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001152 PyObject *self;
1153 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001154{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001155 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001156 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001157 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001158 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001159 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001160 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001161 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001162 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001163 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001164 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001165 u.sysname,
1166 u.nodename,
1167 u.release,
1168 u.version,
1169 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001170}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001171#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001172
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001173
1174static char posix_utime__doc__[] =
1175"utime(path, (atime, utime)) -> None\n\
1176Set the access and modified time of the file to the given values.";
1177
Barry Warsaw53699e91996-12-10 23:23:01 +00001178static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001179posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001180 PyObject *self;
1181 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001182{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001183 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001184 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001185 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001186
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001187/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001188#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001189 struct utimbuf buf;
1190#define ATIME buf.actime
1191#define MTIME buf.modtime
1192#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001193#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001194 time_t buf[2];
1195#define ATIME buf[0]
1196#define MTIME buf[1]
1197#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001198#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001199
Barry Warsaw53699e91996-12-10 23:23:01 +00001200 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001201 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001202 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001203 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001204 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001205 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001206 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001207 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001208 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001209 Py_INCREF(Py_None);
1210 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001211#undef UTIME_ARG
1212#undef ATIME
1213#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001214}
1215
Guido van Rossum85e3b011991-06-03 12:42:10 +00001216
Guido van Rossum3b066191991-06-04 19:40:25 +00001217/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001218
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001219static char posix__exit__doc__[] =
1220"_exit(status)\n\
1221Exit to the system with specified status, without normal exit processing.";
1222
Barry Warsaw53699e91996-12-10 23:23:01 +00001223static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001224posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001225 PyObject *self;
1226 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001227{
1228 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001229 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001230 return NULL;
1231 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001232 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001233}
1234
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001235
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001236#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001237static char posix_execv__doc__[] =
1238"execv(path, args)\n\
1239Execute an executable path with arguments, replacing current process.\n\
1240\n\
1241 path: path of executable file\n\
1242 args: tuple or list of strings";
1243
Barry Warsaw53699e91996-12-10 23:23:01 +00001244static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001245posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001246 PyObject *self;
1247 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001248{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001249 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001250 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001251 char **argvlist;
1252 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001253 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001254
Guido van Rossum89b33251993-10-22 14:26:06 +00001255 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001256 argv is a list or tuple of strings. */
1257
Barry Warsaw53699e91996-12-10 23:23:01 +00001258 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001259 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001260 if (PyList_Check(argv)) {
1261 argc = PyList_Size(argv);
1262 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001263 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001264 else if (PyTuple_Check(argv)) {
1265 argc = PyTuple_Size(argv);
1266 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001267 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001268 else {
1269 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001270 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001271 return NULL;
1272 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001273
Barry Warsaw53699e91996-12-10 23:23:01 +00001274 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001275 if (argvlist == NULL)
1276 return NULL;
1277 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001278 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1279 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001280 goto badarg;
1281 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001282 }
1283 argvlist[argc] = NULL;
1284
Guido van Rossumb6775db1994-08-01 11:34:53 +00001285#ifdef BAD_EXEC_PROTOTYPES
1286 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001287#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001288 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001289#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001290
Guido van Rossum85e3b011991-06-03 12:42:10 +00001291 /* If we get here it's definitely an error */
1292
Barry Warsaw53699e91996-12-10 23:23:01 +00001293 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001294 return posix_error();
1295}
1296
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001297
1298static char posix_execve__doc__[] =
1299"execve(path, args, env)\n\
1300Execute a path with arguments and environment, replacing current process.\n\
1301\n\
1302 path: path of executable file\n\
1303 args: tuple or list of arguments\n\
1304 env: dictonary of strings mapping to strings";
1305
Barry Warsaw53699e91996-12-10 23:23:01 +00001306static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001307posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001308 PyObject *self;
1309 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001310{
1311 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001312 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001313 char **argvlist;
1314 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001315 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001316 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001317 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001318
1319 /* execve has three arguments: (path, argv, env), where
1320 argv is a list or tuple of strings and env is a dictionary
1321 like posix.environ. */
1322
Barry Warsaw53699e91996-12-10 23:23:01 +00001323 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001324 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001325 if (PyList_Check(argv)) {
1326 argc = PyList_Size(argv);
1327 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001328 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001329 else if (PyTuple_Check(argv)) {
1330 argc = PyTuple_Size(argv);
1331 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001332 }
1333 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001334 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001335 return NULL;
1336 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001337 if (!PyMapping_Check(env)) {
1338 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001339 return NULL;
1340 }
1341
Barry Warsaw53699e91996-12-10 23:23:01 +00001342 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001343 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001344 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001345 return NULL;
1346 }
1347 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001348 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001349 "s;argv must be list of strings",
1350 &argvlist[i]))
1351 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001352 goto fail_1;
1353 }
1354 }
1355 argvlist[argc] = NULL;
1356
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001357 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001358 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001359 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001360 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001361 goto fail_1;
1362 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001363 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001364 keys = PyMapping_Keys(env);
1365 vals = PyMapping_Values(env);
1366 if (!keys || !vals)
1367 goto fail_2;
1368
1369 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001370 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001371
1372 key = PyList_GetItem(keys, pos);
1373 val = PyList_GetItem(vals, pos);
1374 if (!key || !val)
1375 goto fail_2;
1376
Barry Warsaw53699e91996-12-10 23:23:01 +00001377 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001378 !PyArg_Parse(val, "s;non-string value in env", &v))
1379 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001380 goto fail_2;
1381 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001382
1383#if defined(PYOS_OS2)
1384 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1385 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1386#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001387 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001388 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001389 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001390 goto fail_2;
1391 }
1392 sprintf(p, "%s=%s", k, v);
1393 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001394#if defined(PYOS_OS2)
1395 }
1396#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001397 }
1398 envlist[envc] = 0;
1399
Guido van Rossumb6775db1994-08-01 11:34:53 +00001400
1401#ifdef BAD_EXEC_PROTOTYPES
1402 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001403#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001404 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001405#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001406
1407 /* If we get here it's definitely an error */
1408
1409 (void) posix_error();
1410
1411 fail_2:
1412 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001413 PyMem_DEL(envlist[envc]);
1414 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001415 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001416 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001417 Py_XDECREF(vals);
1418 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001419 return NULL;
1420}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001421#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001422
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001423
Guido van Rossuma1065681999-01-25 23:20:23 +00001424#ifdef HAVE_SPAWNV
1425static char posix_spawnv__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001426"spawnv(mode, path, args)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001427Execute an executable path with arguments, replacing current process.\n\
1428\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001429 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001430 path: path of executable file\n\
1431 args: tuple or list of strings";
1432
1433static PyObject *
1434posix_spawnv(self, args)
1435 PyObject *self;
1436 PyObject *args;
1437{
1438 char *path;
1439 PyObject *argv;
1440 char **argvlist;
1441 int mode, i, argc;
1442 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1443
1444 /* spawnv has three arguments: (mode, path, argv), where
1445 argv is a list or tuple of strings. */
1446
1447 if (!PyArg_Parse(args, "(isO)", &mode, &path, &argv))
1448 return NULL;
1449 if (PyList_Check(argv)) {
1450 argc = PyList_Size(argv);
1451 getitem = PyList_GetItem;
1452 }
1453 else if (PyTuple_Check(argv)) {
1454 argc = PyTuple_Size(argv);
1455 getitem = PyTuple_GetItem;
1456 }
1457 else {
1458 badarg:
1459 PyErr_BadArgument();
1460 return NULL;
1461 }
1462
1463 argvlist = PyMem_NEW(char *, argc+1);
1464 if (argvlist == NULL)
1465 return NULL;
1466 for (i = 0; i < argc; i++) {
1467 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1468 PyMem_DEL(argvlist);
1469 goto badarg;
1470 }
1471 }
1472 argvlist[argc] = NULL;
1473
Guido van Rossum246bc171999-02-01 23:54:31 +00001474 if (mode == _OLD_P_OVERLAY)
1475 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001476 i = _spawnv(mode, path, argvlist);
1477
1478 PyMem_DEL(argvlist);
1479
1480 if (i == -1)
1481 return posix_error();
1482 else
1483 return Py_BuildValue("i", i);
1484}
1485
1486
1487static char posix_spawnve__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001488"spawnve(mode, path, args, env)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001489Execute a path with arguments and environment, replacing current process.\n\
1490\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001491 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001492 path: path of executable file\n\
1493 args: tuple or list of arguments\n\
1494 env: dictonary of strings mapping to strings";
1495
1496static PyObject *
1497posix_spawnve(self, args)
1498 PyObject *self;
1499 PyObject *args;
1500{
1501 char *path;
1502 PyObject *argv, *env;
1503 char **argvlist;
1504 char **envlist;
1505 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1506 int mode, i, pos, argc, envc;
1507 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1508
1509 /* spawnve has four arguments: (mode, path, argv, env), where
1510 argv is a list or tuple of strings and env is a dictionary
1511 like posix.environ. */
1512
1513 if (!PyArg_Parse(args, "(isOO)", &mode, &path, &argv, &env))
1514 return NULL;
1515 if (PyList_Check(argv)) {
1516 argc = PyList_Size(argv);
1517 getitem = PyList_GetItem;
1518 }
1519 else if (PyTuple_Check(argv)) {
1520 argc = PyTuple_Size(argv);
1521 getitem = PyTuple_GetItem;
1522 }
1523 else {
1524 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1525 return NULL;
1526 }
1527 if (!PyMapping_Check(env)) {
1528 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1529 return NULL;
1530 }
1531
1532 argvlist = PyMem_NEW(char *, argc+1);
1533 if (argvlist == NULL) {
1534 PyErr_NoMemory();
1535 return NULL;
1536 }
1537 for (i = 0; i < argc; i++) {
1538 if (!PyArg_Parse((*getitem)(argv, i),
1539 "s;argv must be list of strings",
1540 &argvlist[i]))
1541 {
1542 goto fail_1;
1543 }
1544 }
1545 argvlist[argc] = NULL;
1546
1547 i = PyMapping_Length(env);
1548 envlist = PyMem_NEW(char *, i + 1);
1549 if (envlist == NULL) {
1550 PyErr_NoMemory();
1551 goto fail_1;
1552 }
1553 envc = 0;
1554 keys = PyMapping_Keys(env);
1555 vals = PyMapping_Values(env);
1556 if (!keys || !vals)
1557 goto fail_2;
1558
1559 for (pos = 0; pos < i; pos++) {
1560 char *p, *k, *v;
1561
1562 key = PyList_GetItem(keys, pos);
1563 val = PyList_GetItem(vals, pos);
1564 if (!key || !val)
1565 goto fail_2;
1566
1567 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1568 !PyArg_Parse(val, "s;non-string value in env", &v))
1569 {
1570 goto fail_2;
1571 }
1572 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1573 if (p == NULL) {
1574 PyErr_NoMemory();
1575 goto fail_2;
1576 }
1577 sprintf(p, "%s=%s", k, v);
1578 envlist[envc++] = p;
1579 }
1580 envlist[envc] = 0;
1581
Guido van Rossum246bc171999-02-01 23:54:31 +00001582 if (mode == _OLD_P_OVERLAY)
1583 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001584 i = _spawnve(mode, path, argvlist, envlist);
1585 if (i == -1)
1586 (void) posix_error();
1587 else
1588 res = Py_BuildValue("i", i);
1589
1590 fail_2:
1591 while (--envc >= 0)
1592 PyMem_DEL(envlist[envc]);
1593 PyMem_DEL(envlist);
1594 fail_1:
1595 PyMem_DEL(argvlist);
1596 Py_XDECREF(vals);
1597 Py_XDECREF(keys);
1598 return res;
1599}
1600#endif /* HAVE_SPAWNV */
1601
1602
Guido van Rossumad0ee831995-03-01 10:34:45 +00001603#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001604static char posix_fork__doc__[] =
1605"fork() -> pid\n\
1606Fork a child process.\n\
1607\n\
1608Return 0 to child process and PID of child to parent process.";
1609
Barry Warsaw53699e91996-12-10 23:23:01 +00001610static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001611posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001612 PyObject *self;
1613 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001614{
1615 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001616 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001617 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001618 pid = fork();
1619 if (pid == -1)
1620 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001621 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001622 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001623}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001624#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001625
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001626
Guido van Rossumad0ee831995-03-01 10:34:45 +00001627#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001628static char posix_getegid__doc__[] =
1629"getegid() -> egid\n\
1630Return the current process's effective group id.";
1631
Barry Warsaw53699e91996-12-10 23:23:01 +00001632static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001633posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001634 PyObject *self;
1635 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001636{
Barry Warsaw53699e91996-12-10 23:23:01 +00001637 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001638 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001639 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001640}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001641#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001642
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001643
Guido van Rossumad0ee831995-03-01 10:34:45 +00001644#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001645static char posix_geteuid__doc__[] =
1646"geteuid() -> euid\n\
1647Return the current process's effective user id.";
1648
Barry Warsaw53699e91996-12-10 23:23:01 +00001649static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001650posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001651 PyObject *self;
1652 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001653{
Barry Warsaw53699e91996-12-10 23:23:01 +00001654 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001655 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001656 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001657}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001658#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001659
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001660
Guido van Rossumad0ee831995-03-01 10:34:45 +00001661#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001662static char posix_getgid__doc__[] =
1663"getgid() -> gid\n\
1664Return the current process's group id.";
1665
Barry Warsaw53699e91996-12-10 23:23:01 +00001666static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001667posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001668 PyObject *self;
1669 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001670{
Barry Warsaw53699e91996-12-10 23:23:01 +00001671 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001672 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001673 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001674}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001675#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001676
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001677
1678static char posix_getpid__doc__[] =
1679"getpid() -> pid\n\
1680Return the current process id";
1681
Barry Warsaw53699e91996-12-10 23:23:01 +00001682static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001683posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001684 PyObject *self;
1685 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001686{
Barry Warsaw53699e91996-12-10 23:23:01 +00001687 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001688 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001689 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001690}
1691
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001692
Guido van Rossumb6775db1994-08-01 11:34:53 +00001693#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001694static char posix_getpgrp__doc__[] =
1695"getpgrp() -> pgrp\n\
1696Return the current process group id.";
1697
Barry Warsaw53699e91996-12-10 23:23:01 +00001698static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001699posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001700 PyObject *self;
1701 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001702{
Barry Warsaw53699e91996-12-10 23:23:01 +00001703 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001704 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001705#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001706 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001707#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001708 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001709#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001710}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001711#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001712
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001713
Guido van Rossumb6775db1994-08-01 11:34:53 +00001714#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001715static char posix_setpgrp__doc__[] =
1716"setpgrp() -> None\n\
1717Make this process a session leader.";
1718
Barry Warsaw53699e91996-12-10 23:23:01 +00001719static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001720posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001721 PyObject *self;
1722 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001723{
Barry Warsaw53699e91996-12-10 23:23:01 +00001724 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001725 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001726#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001727 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001728#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001729 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001730#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001731 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001732 Py_INCREF(Py_None);
1733 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001734}
1735
Guido van Rossumb6775db1994-08-01 11:34:53 +00001736#endif /* HAVE_SETPGRP */
1737
Guido van Rossumad0ee831995-03-01 10:34:45 +00001738#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001739static char posix_getppid__doc__[] =
1740"getppid() -> ppid\n\
1741Return the parent's process id.";
1742
Barry Warsaw53699e91996-12-10 23:23:01 +00001743static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001744posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001745 PyObject *self;
1746 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001747{
Barry Warsaw53699e91996-12-10 23:23:01 +00001748 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001749 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001750 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001751}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001752#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001753
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001754
Guido van Rossumad0ee831995-03-01 10:34:45 +00001755#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001756static char posix_getuid__doc__[] =
1757"getuid() -> uid\n\
1758Return the current process's user id.";
1759
Barry Warsaw53699e91996-12-10 23:23:01 +00001760static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001761posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001762 PyObject *self;
1763 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001764{
Barry Warsaw53699e91996-12-10 23:23:01 +00001765 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001766 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001767 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001768}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001769#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001770
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001771
Guido van Rossumad0ee831995-03-01 10:34:45 +00001772#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001773static char posix_kill__doc__[] =
1774"kill(pid, sig) -> None\n\
1775Kill a process with a signal.";
1776
Barry Warsaw53699e91996-12-10 23:23:01 +00001777static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001778posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001779 PyObject *self;
1780 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001781{
1782 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001783 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001784 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001785#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001786 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1787 APIRET rc;
1788 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001789 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001790
1791 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1792 APIRET rc;
1793 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001794 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001795
1796 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001797 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001798#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001799 if (kill(pid, sig) == -1)
1800 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001801#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001802 Py_INCREF(Py_None);
1803 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001804}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001805#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001806
Guido van Rossumc0125471996-06-28 18:55:32 +00001807#ifdef HAVE_PLOCK
1808
1809#ifdef HAVE_SYS_LOCK_H
1810#include <sys/lock.h>
1811#endif
1812
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001813static char posix_plock__doc__[] =
1814"plock(op) -> None\n\
1815Lock program segments into memory.";
1816
Barry Warsaw53699e91996-12-10 23:23:01 +00001817static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001818posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001819 PyObject *self;
1820 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001821{
1822 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001823 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001824 return NULL;
1825 if (plock(op) == -1)
1826 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001827 Py_INCREF(Py_None);
1828 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001829}
1830#endif
1831
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001832
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001833#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001834static char posix_popen__doc__[] =
1835"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1836Open a pipe to/from a command returning a file object.";
1837
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001838#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001839static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001840async_system(const char *command)
1841{
1842 char *p, errormsg[256], args[1024];
1843 RESULTCODES rcodes;
1844 APIRET rc;
1845 char *shell = getenv("COMSPEC");
1846 if (!shell)
1847 shell = "cmd";
1848
1849 strcpy(args, shell);
1850 p = &args[ strlen(args)+1 ];
1851 strcpy(p, "/c ");
1852 strcat(p, command);
1853 p += strlen(p) + 1;
1854 *p = '\0';
1855
1856 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001857 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001858 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001859 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001860 &rcodes, shell);
1861 return rc;
1862}
1863
Guido van Rossumd48f2521997-12-05 22:19:34 +00001864static FILE *
1865popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001866{
1867 HFILE rhan, whan;
1868 FILE *retfd = NULL;
1869 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1870
Guido van Rossumd48f2521997-12-05 22:19:34 +00001871 if (rc != NO_ERROR) {
1872 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001873 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001874 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001875
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001876 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1877 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001878
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001879 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1880 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001881
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001882 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1883 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001884
1885 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001886 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001887 }
1888
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001889 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1890 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001891
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001892 close(oldfd); /* And Close Saved STDOUT Handle */
1893 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001894
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001895 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1896 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001897
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001898 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1899 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001900
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001901 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1902 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001903
1904 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001905 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001906 }
1907
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001908 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1909 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001910
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001911 close(oldfd); /* And Close Saved STDIN Handle */
1912 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001913
Guido van Rossumd48f2521997-12-05 22:19:34 +00001914 } else {
1915 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001916 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001917 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001918}
1919
1920static PyObject *
1921posix_popen(self, args)
1922 PyObject *self;
1923 PyObject *args;
1924{
1925 char *name;
1926 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001927 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001928 FILE *fp;
1929 PyObject *f;
1930 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1931 return NULL;
1932 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001933 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001934 Py_END_ALLOW_THREADS
1935 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001936 return os2_error(err);
1937
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001938 f = PyFile_FromFile(fp, name, mode, fclose);
1939 if (f != NULL)
1940 PyFile_SetBufSize(f, bufsize);
1941 return f;
1942}
1943
1944#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001945static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001946posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001947 PyObject *self;
1948 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001949{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001950 char *name;
1951 char *mode = "r";
1952 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001953 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001954 PyObject *f;
1955 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001956 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001957 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001958 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001959 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001960 if (fp == NULL)
1961 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001962 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001963 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001964 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001965 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001966}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001967#endif
1968
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001969#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001970
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001971
Guido van Rossumb6775db1994-08-01 11:34:53 +00001972#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001973static char posix_setuid__doc__[] =
1974"setuid(uid) -> None\n\
1975Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001976static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001977posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001978 PyObject *self;
1979 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001980{
1981 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001982 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001983 return NULL;
1984 if (setuid(uid) < 0)
1985 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001986 Py_INCREF(Py_None);
1987 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001988}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001989#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001990
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001991
Guido van Rossumb6775db1994-08-01 11:34:53 +00001992#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001993static char posix_setgid__doc__[] =
1994"setgid(gid) -> None\n\
1995Set the current process's group id.";
1996
Barry Warsaw53699e91996-12-10 23:23:01 +00001997static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001998posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001999 PyObject *self;
2000 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002001{
2002 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002003 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002004 return NULL;
2005 if (setgid(gid) < 0)
2006 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002007 Py_INCREF(Py_None);
2008 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002009}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002010#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002011
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002012
Guido van Rossumb6775db1994-08-01 11:34:53 +00002013#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002014static char posix_waitpid__doc__[] =
2015"waitpid(pid, options) -> (pid, status)\n\
2016Wait for completion of a give child process.";
2017
Barry Warsaw53699e91996-12-10 23:23:01 +00002018static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002019posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002020 PyObject *self;
2021 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002022{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002023 int pid, options;
2024#ifdef UNION_WAIT
2025 union wait status;
2026#define status_i (status.w_status)
2027#else
2028 int status;
2029#define status_i status
2030#endif
2031 status_i = 0;
2032
Barry Warsaw53699e91996-12-10 23:23:01 +00002033 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002034 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002035 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002036#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002037 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002038#else
2039 pid = waitpid(pid, &status, options);
2040#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002041 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002042 if (pid == -1)
2043 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002044 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002045 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002046}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002047#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002049
Guido van Rossumad0ee831995-03-01 10:34:45 +00002050#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002051static char posix_wait__doc__[] =
2052"wait() -> (pid, status)\n\
2053Wait for completion of a child process.";
2054
Barry Warsaw53699e91996-12-10 23:23:01 +00002055static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002056posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002057 PyObject *self;
2058 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00002059{
2060 int pid, sts;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002061#ifdef UNION_WAIT
2062 union wait status;
2063#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002064#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002065 int status;
2066#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002067#endif
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002068 status_i = 0;
2069 Py_BEGIN_ALLOW_THREADS
2070 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00002071 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00002072 if (pid == -1)
2073 return posix_error();
2074 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002075 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002076}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002077#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002078
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002079
2080static char posix_lstat__doc__[] =
2081"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
2082Like stat(path), but do not follow symbolic links.";
2083
Barry Warsaw53699e91996-12-10 23:23:01 +00002084static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002085posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002086 PyObject *self;
2087 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002088{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002089#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002090 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002091#else /* !HAVE_LSTAT */
2092 return posix_do_stat(self, args, stat);
2093#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002094}
2095
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002096
Guido van Rossumb6775db1994-08-01 11:34:53 +00002097#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002098static char posix_readlink__doc__[] =
2099"readlink(path) -> path\n\
2100Return a string representing the path to which the symbolic link points.";
2101
Barry Warsaw53699e91996-12-10 23:23:01 +00002102static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002103posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002104 PyObject *self;
2105 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002106{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002107 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002108 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002109 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002110 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002111 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002112 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00002113 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00002114 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002115 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002116 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002117 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002118}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002119#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002120
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002121
Guido van Rossumb6775db1994-08-01 11:34:53 +00002122#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002123static char posix_symlink__doc__[] =
2124"symlink(src, dst) -> None\n\
2125Create a symbolic link.";
2126
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002127static PyObject *
2128posix_symlink(self, args)
2129 PyObject *self;
2130 PyObject *args;
2131{
2132 return posix_2str(args, symlink);
2133}
2134#endif /* HAVE_SYMLINK */
2135
2136
2137#ifdef HAVE_TIMES
2138#ifndef HZ
2139#define HZ 60 /* Universal constant :-) */
2140#endif /* HZ */
2141
Guido van Rossumd48f2521997-12-05 22:19:34 +00002142#if defined(PYCC_VACPP) && defined(PYOS_OS2)
2143static long
2144system_uptime()
2145{
2146 ULONG value = 0;
2147
2148 Py_BEGIN_ALLOW_THREADS
2149 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
2150 Py_END_ALLOW_THREADS
2151
2152 return value;
2153}
2154
2155static PyObject *
2156posix_times(self, args)
2157 PyObject *self;
2158 PyObject *args;
2159{
2160 if (!PyArg_NoArgs(args))
2161 return NULL;
2162
2163 /* Currently Only Uptime is Provided -- Others Later */
2164 return Py_BuildValue("ddddd",
2165 (double)0 /* t.tms_utime / HZ */,
2166 (double)0 /* t.tms_stime / HZ */,
2167 (double)0 /* t.tms_cutime / HZ */,
2168 (double)0 /* t.tms_cstime / HZ */,
2169 (double)system_uptime() / 1000);
2170}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002171#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002172static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00002173posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002174 PyObject *self;
2175 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00002176{
2177 struct tms t;
2178 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00002179 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00002180 return NULL;
2181 errno = 0;
2182 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00002183 if (c == (clock_t) -1)
2184 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002185 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002186 (double)t.tms_utime / HZ,
2187 (double)t.tms_stime / HZ,
2188 (double)t.tms_cutime / HZ,
2189 (double)t.tms_cstime / HZ,
2190 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00002191}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002192#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002193#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002194
2195
Guido van Rossum87755a21996-09-07 00:59:43 +00002196#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002197#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00002198static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002199posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002200 PyObject *self;
2201 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002202{
2203 FILETIME create, exit, kernel, user;
2204 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00002205 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002206 return NULL;
2207 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002208 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
2209 /* The fields of a FILETIME structure are the hi and lo part
2210 of a 64-bit value expressed in 100 nanosecond units.
2211 1e7 is one second in such units; 1e-7 the inverse.
2212 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
2213 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002214 return Py_BuildValue(
2215 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002216 (double)(kernel.dwHighDateTime*429.4967296 +
2217 kernel.dwLowDateTime*1e-7),
2218 (double)(user.dwHighDateTime*429.4967296 +
2219 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00002220 (double)0,
2221 (double)0,
2222 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002223}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002224#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002225
2226#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00002227static char posix_times__doc__[] =
2228"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2229Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002230#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002231
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002232
Guido van Rossumb6775db1994-08-01 11:34:53 +00002233#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002234static char posix_setsid__doc__[] =
2235"setsid() -> None\n\
2236Call the system call setsid().";
2237
Barry Warsaw53699e91996-12-10 23:23:01 +00002238static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002239posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002240 PyObject *self;
2241 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002242{
Barry Warsaw53699e91996-12-10 23:23:01 +00002243 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002244 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002245 if (setsid() < 0)
2246 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002247 Py_INCREF(Py_None);
2248 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002249}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002250#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002251
Guido van Rossumb6775db1994-08-01 11:34:53 +00002252#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002253static char posix_setpgid__doc__[] =
2254"setpgid(pid, pgrp) -> None\n\
2255Call the system call setpgid().";
2256
Barry Warsaw53699e91996-12-10 23:23:01 +00002257static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002258posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002259 PyObject *self;
2260 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002261{
2262 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002263 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002264 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002265 if (setpgid(pid, pgrp) < 0)
2266 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002267 Py_INCREF(Py_None);
2268 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002269}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002270#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002271
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002272
Guido van Rossumb6775db1994-08-01 11:34:53 +00002273#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002274static char posix_tcgetpgrp__doc__[] =
2275"tcgetpgrp(fd) -> pgid\n\
2276Return the process group associated with the terminal given by a fd.";
2277
Barry Warsaw53699e91996-12-10 23:23:01 +00002278static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002279posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002280 PyObject *self;
2281 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002282{
2283 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002284 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002285 return NULL;
2286 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002287 if (pgid < 0)
2288 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002289 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002290}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002291#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002292
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002293
Guido van Rossumb6775db1994-08-01 11:34:53 +00002294#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002295static char posix_tcsetpgrp__doc__[] =
2296"tcsetpgrp(fd, pgid) -> None\n\
2297Set the process group associated with the terminal given by a fd.";
2298
Barry Warsaw53699e91996-12-10 23:23:01 +00002299static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002300posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002301 PyObject *self;
2302 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002303{
2304 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002305 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002306 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002307 if (tcsetpgrp(fd, pgid) < 0)
2308 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002309 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002310 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002311}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002312#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002313
Guido van Rossum687dd131993-05-17 08:34:16 +00002314/* Functions acting on file descriptors */
2315
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002316static char posix_open__doc__[] =
2317"open(filename, flag [, mode=0777]) -> fd\n\
2318Open a file (for low level IO).";
2319
Barry Warsaw53699e91996-12-10 23:23:01 +00002320static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002321posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002322 PyObject *self;
2323 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002324{
2325 char *file;
2326 int flag;
2327 int mode = 0777;
2328 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002329 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2330 return NULL;
2331
Barry Warsaw53699e91996-12-10 23:23:01 +00002332 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002333 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002334 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002335 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002336 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002337 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002338}
2339
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002340
2341static char posix_close__doc__[] =
2342"close(fd) -> None\n\
2343Close a file descriptor (for low level IO).";
2344
Barry Warsaw53699e91996-12-10 23:23:01 +00002345static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002346posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002347 PyObject *self;
2348 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002349{
2350 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002351 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002352 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002353 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002354 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002355 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002356 if (res < 0)
2357 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002358 Py_INCREF(Py_None);
2359 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002360}
2361
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002362
2363static char posix_dup__doc__[] =
2364"dup(fd) -> fd2\n\
2365Return a duplicate of a file descriptor.";
2366
Barry Warsaw53699e91996-12-10 23:23:01 +00002367static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002368posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002369 PyObject *self;
2370 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002371{
2372 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002373 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002374 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002375 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002376 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002377 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002378 if (fd < 0)
2379 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002380 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002381}
2382
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002383
2384static char posix_dup2__doc__[] =
2385"dup2(fd, fd2) -> None\n\
2386Duplicate file descriptor.";
2387
Barry Warsaw53699e91996-12-10 23:23:01 +00002388static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002389posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002390 PyObject *self;
2391 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002392{
2393 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002394 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002395 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002396 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002397 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002398 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002399 if (res < 0)
2400 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002401 Py_INCREF(Py_None);
2402 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002403}
2404
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002405
2406static char posix_lseek__doc__[] =
2407"lseek(fd, pos, how) -> newpos\n\
2408Set the current position of a file descriptor.";
2409
Barry Warsaw53699e91996-12-10 23:23:01 +00002410static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002411posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002412 PyObject *self;
2413 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002414{
2415 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002416 off_t pos, res;
2417 PyObject *posobj;
2418 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002419 return NULL;
2420#ifdef SEEK_SET
2421 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2422 switch (how) {
2423 case 0: how = SEEK_SET; break;
2424 case 1: how = SEEK_CUR; break;
2425 case 2: how = SEEK_END; break;
2426 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002427#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002428
2429#if !defined(HAVE_LARGEFILE_SUPPORT)
2430 pos = PyInt_AsLong(posobj);
2431#else
2432 pos = PyLong_Check(posobj) ?
2433 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2434#endif
2435 if (PyErr_Occurred())
2436 return NULL;
2437
Barry Warsaw53699e91996-12-10 23:23:01 +00002438 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002439 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002440 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002441 if (res < 0)
2442 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002443
2444#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002445 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002446#else
2447 return PyLong_FromLongLong(res);
2448#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002449}
2450
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002451
2452static char posix_read__doc__[] =
2453"read(fd, buffersize) -> string\n\
2454Read a file descriptor.";
2455
Barry Warsaw53699e91996-12-10 23:23:01 +00002456static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002457posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002458 PyObject *self;
2459 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002460{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002461 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002462 PyObject *buffer;
2463 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002464 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002465 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002466 if (buffer == NULL)
2467 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002468 Py_BEGIN_ALLOW_THREADS
2469 n = read(fd, PyString_AsString(buffer), size);
2470 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002471 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002472 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002473 return posix_error();
2474 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002475 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002476 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002477 return buffer;
2478}
2479
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002480
2481static char posix_write__doc__[] =
2482"write(fd, string) -> byteswritten\n\
2483Write a string to a file descriptor.";
2484
Barry Warsaw53699e91996-12-10 23:23:01 +00002485static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002486posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002487 PyObject *self;
2488 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002489{
2490 int fd, size;
2491 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002492 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002493 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002494 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002495 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002496 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002497 if (size < 0)
2498 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002499 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002500}
2501
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002502
2503static char posix_fstat__doc__[]=
2504"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2505Like stat(), but for an open file descriptor.";
2506
Barry Warsaw53699e91996-12-10 23:23:01 +00002507static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002508posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002509 PyObject *self;
2510 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002511{
2512 int fd;
2513 struct stat st;
2514 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002515 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002516 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002517 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002518 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002519 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002520 if (res != 0)
2521 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002522#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002523 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002524 (long)st.st_mode,
2525 (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)st.st_size,
2531 (long)st.st_atime,
2532 (long)st.st_mtime,
2533 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002534#else
2535 return Py_BuildValue("(lLllllLlll)",
2536 (long)st.st_mode,
2537 (LONG_LONG)st.st_ino,
2538 (long)st.st_dev,
2539 (long)st.st_nlink,
2540 (long)st.st_uid,
2541 (long)st.st_gid,
2542 (LONG_LONG)st.st_size,
2543 (long)st.st_atime,
2544 (long)st.st_mtime,
2545 (long)st.st_ctime);
2546#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002547}
2548
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002549
2550static char posix_fdopen__doc__[] =
2551"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2552Return an open file object connected to a file descriptor.";
2553
Barry Warsaw53699e91996-12-10 23:23:01 +00002554static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002555posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002556 PyObject *self;
2557 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002558{
Barry Warsaw53699e91996-12-10 23:23:01 +00002559 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002560 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002561 char *mode = "r";
2562 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002563 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002564 PyObject *f;
2565 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002566 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002567
Barry Warsaw53699e91996-12-10 23:23:01 +00002568 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002569 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002570 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002571 if (fp == NULL)
2572 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002573 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002574 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002575 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002576 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002577}
2578
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002579
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002580#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002581static char posix_pipe__doc__[] =
2582"pipe() -> (read_end, write_end)\n\
2583Create a pipe.";
2584
Barry Warsaw53699e91996-12-10 23:23:01 +00002585static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002586posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002587 PyObject *self;
2588 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002589{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002590#if defined(PYOS_OS2)
2591 HFILE read, write;
2592 APIRET rc;
2593
2594 if (!PyArg_Parse(args, ""))
2595 return NULL;
2596
2597 Py_BEGIN_ALLOW_THREADS
2598 rc = DosCreatePipe( &read, &write, 4096);
2599 Py_END_ALLOW_THREADS
2600 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002601 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002602
2603 return Py_BuildValue("(ii)", read, write);
2604#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002605#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002606 int fds[2];
2607 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002608 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002609 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002610 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002611 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002612 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002613 if (res != 0)
2614 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002615 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002616#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002617 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002618 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002619 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002620 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002621 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002622 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002623 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002624 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002625 if (!ok)
2626 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002627 read_fd = _open_osfhandle((long)read, 0);
2628 write_fd = _open_osfhandle((long)write, 1);
2629 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002630#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002631#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002632}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002633#endif /* HAVE_PIPE */
2634
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002635
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002636#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002637static char posix_mkfifo__doc__[] =
2638"mkfifo(file, [, mode=0666]) -> None\n\
2639Create a FIFO (a POSIX named pipe).";
2640
Barry Warsaw53699e91996-12-10 23:23:01 +00002641static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002642posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002643 PyObject *self;
2644 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002645{
2646 char *file;
2647 int mode = 0666;
2648 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002649 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002650 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002651 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002652 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002653 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002654 if (res < 0)
2655 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002656 Py_INCREF(Py_None);
2657 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002658}
2659#endif
2660
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002661
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002662#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002663static char posix_ftruncate__doc__[] =
2664"ftruncate(fd, length) -> None\n\
2665Truncate a file to a specified length.";
2666
Barry Warsaw53699e91996-12-10 23:23:01 +00002667static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002668posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002669 PyObject *self; /* Not used */
2670 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002671{
2672 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002673 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002674 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002675 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002676
Guido van Rossum94f6f721999-01-06 18:42:14 +00002677 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2678 return NULL;
2679
2680#if !defined(HAVE_LARGEFILE_SUPPORT)
2681 length = PyInt_AsLong(lenobj);
2682#else
2683 length = PyLong_Check(lenobj) ?
2684 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2685#endif
2686 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002687 return NULL;
2688
Barry Warsaw53699e91996-12-10 23:23:01 +00002689 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002690 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002691 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002692 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002693 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002694 return NULL;
2695 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002696 Py_INCREF(Py_None);
2697 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002698}
2699#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002700
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002701#ifdef NeXT
2702#define HAVE_PUTENV
2703/* Steve Spicklemire got this putenv from NeXTAnswers */
2704static int
2705putenv(char *newval)
2706{
2707 extern char **environ;
2708
2709 static int firstTime = 1;
2710 char **ep;
2711 char *cp;
2712 int esiz;
2713 char *np;
2714
2715 if (!(np = strchr(newval, '=')))
2716 return 1;
2717 *np = '\0';
2718
2719 /* look it up */
2720 for (ep=environ ; *ep ; ep++)
2721 {
2722 /* this should always be true... */
2723 if (cp = strchr(*ep, '='))
2724 {
2725 *cp = '\0';
2726 if (!strcmp(*ep, newval))
2727 {
2728 /* got it! */
2729 *cp = '=';
2730 break;
2731 }
2732 *cp = '=';
2733 }
2734 else
2735 {
2736 *np = '=';
2737 return 1;
2738 }
2739 }
2740
2741 *np = '=';
2742 if (*ep)
2743 {
2744 /* the string was already there:
2745 just replace it with the new one */
2746 *ep = newval;
2747 return 0;
2748 }
2749
2750 /* expand environ by one */
2751 for (esiz=2, ep=environ ; *ep ; ep++)
2752 esiz++;
2753 if (firstTime)
2754 {
2755 char **epp;
2756 char **newenv;
2757 if (!(newenv = malloc(esiz * sizeof(char *))))
2758 return 1;
2759
2760 for (ep=environ, epp=newenv ; *ep ;)
2761 *epp++ = *ep++;
2762 *epp++ = newval;
2763 *epp = (char *) 0;
2764 environ = newenv;
2765 }
2766 else
2767 {
2768 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2769 return 1;
2770 environ[esiz - 2] = newval;
2771 environ[esiz - 1] = (char *) 0;
2772 firstTime = 0;
2773 }
2774
2775 return 0;
2776}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002777#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002778
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002779
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002780#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002781static char posix_putenv__doc__[] =
2782"putenv(key, value) -> None\n\
2783Change or add an environment variable.";
2784
Guido van Rossumbcc20741998-08-04 22:53:56 +00002785#ifdef __BEOS__
2786/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2787int putenv( const char *str );
2788#endif
2789
Barry Warsaw53699e91996-12-10 23:23:01 +00002790static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002791posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002792 PyObject *self;
2793 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002794{
2795 char *s1, *s2;
2796 char *new;
2797
Barry Warsaw53699e91996-12-10 23:23:01 +00002798 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002799 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002800
2801#if defined(PYOS_OS2)
2802 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2803 APIRET rc;
2804
2805 if (strlen(s2) == 0) /* If New Value is an Empty String */
2806 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2807
2808 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2809 if (rc != NO_ERROR)
2810 return os2_error(rc);
2811
2812 } else if (stricmp(s1, "ENDLIBPATH") == 0) {
2813 APIRET rc;
2814
2815 if (strlen(s2) == 0) /* If New Value is an Empty String */
2816 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2817
2818 rc = DosSetExtLIBPATH(s2, END_LIBPATH);
2819 if (rc != NO_ERROR)
2820 return os2_error(rc);
2821 } else {
2822#endif
2823
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002824 /* XXX This leaks memory -- not easy to fix :-( */
2825 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002826 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002827 (void) sprintf(new, "%s=%s", s1, s2);
2828 if (putenv(new)) {
2829 posix_error();
2830 return NULL;
2831 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002832
2833#if defined(PYOS_OS2)
2834 }
2835#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002836 Py_INCREF(Py_None);
2837 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002838}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002839#endif /* putenv */
2840
2841#ifdef HAVE_STRERROR
2842static char posix_strerror__doc__[] =
2843"strerror(code) -> string\n\
2844Translate an error code to a message string.";
2845
2846PyObject *
2847posix_strerror(self, args)
2848 PyObject *self;
2849 PyObject *args;
2850{
2851 int code;
2852 char *message;
2853 if (!PyArg_ParseTuple(args, "i", &code))
2854 return NULL;
2855 message = strerror(code);
2856 if (message == NULL) {
2857 PyErr_SetString(PyExc_ValueError,
2858 "strerror code out of range");
2859 return NULL;
2860 }
2861 return PyString_FromString(message);
2862}
2863#endif /* strerror */
2864
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002865
Guido van Rossumc9641791998-08-04 15:26:23 +00002866#ifdef HAVE_SYS_WAIT_H
2867
2868#ifdef WIFSTOPPED
2869static char posix_WIFSTOPPED__doc__[] =
2870"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002871Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002872
2873static PyObject *
2874posix_WIFSTOPPED(self, args)
2875 PyObject *self;
2876 PyObject *args;
2877{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002878#ifdef UNION_WAIT
2879 union wait status;
2880#define status_i (status.w_status)
2881#else
2882 int status;
2883#define status_i status
2884#endif
2885 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002886
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002887 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002888 {
2889 return NULL;
2890 }
2891
2892 return Py_BuildValue("i", WIFSTOPPED(status));
2893}
2894#endif /* WIFSTOPPED */
2895
2896#ifdef WIFSIGNALED
2897static char posix_WIFSIGNALED__doc__[] =
2898"WIFSIGNALED(status) -> Boolean\n\
Guido van Rossum3366d1c1999-02-23 18:34:43 +00002899Return true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002900
2901static PyObject *
2902posix_WIFSIGNALED(self, args)
2903 PyObject *self;
2904 PyObject *args;
2905{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002906#ifdef UNION_WAIT
2907 union wait status;
2908#define status_i (status.w_status)
2909#else
2910 int status;
2911#define status_i status
2912#endif
2913 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002914
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002915 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002916 {
2917 return NULL;
2918 }
2919
2920 return Py_BuildValue("i", WIFSIGNALED(status));
2921}
2922#endif /* WIFSIGNALED */
2923
2924#ifdef WIFEXITED
2925static char posix_WIFEXITED__doc__[] =
2926"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002927Return true if the process returning 'status' exited using the exit()\n\
2928system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002929
2930static PyObject *
2931posix_WIFEXITED(self, args)
2932 PyObject *self;
2933 PyObject *args;
2934{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002935#ifdef UNION_WAIT
2936 union wait status;
2937#define status_i (status.w_status)
2938#else
2939 int status;
2940#define status_i status
2941#endif
2942 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002943
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002944 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002945 {
2946 return NULL;
2947 }
2948
2949 return Py_BuildValue("i", WIFEXITED(status));
2950}
2951#endif /* WIFEXITED */
2952
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002953#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00002954static char posix_WEXITSTATUS__doc__[] =
2955"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002956Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002957
2958static PyObject *
2959posix_WEXITSTATUS(self, args)
2960 PyObject *self;
2961 PyObject *args;
2962{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002963#ifdef UNION_WAIT
2964 union wait status;
2965#define status_i (status.w_status)
2966#else
2967 int status;
2968#define status_i status
2969#endif
2970 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002971
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002972 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002973 {
2974 return NULL;
2975 }
2976
2977 return Py_BuildValue("i", WEXITSTATUS(status));
2978}
2979#endif /* WEXITSTATUS */
2980
2981#ifdef WTERMSIG
2982static char posix_WTERMSIG__doc__[] =
2983"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002984Return the signal that terminated the process that provided the 'status'\n\
2985value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002986
2987static PyObject *
2988posix_WTERMSIG(self, args)
2989 PyObject *self;
2990 PyObject *args;
2991{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002992#ifdef UNION_WAIT
2993 union wait status;
2994#define status_i (status.w_status)
2995#else
2996 int status;
2997#define status_i status
2998#endif
2999 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003000
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003001 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003002 {
3003 return NULL;
3004 }
3005
3006 return Py_BuildValue("i", WTERMSIG(status));
3007}
3008#endif /* WTERMSIG */
3009
3010#ifdef WSTOPSIG
3011static char posix_WSTOPSIG__doc__[] =
3012"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003013Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003014
3015static PyObject *
3016posix_WSTOPSIG(self, args)
3017 PyObject *self;
3018 PyObject *args;
3019{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003020#ifdef UNION_WAIT
3021 union wait status;
3022#define status_i (status.w_status)
3023#else
3024 int status;
3025#define status_i status
3026#endif
3027 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003028
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003029 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003030 {
3031 return NULL;
3032 }
3033
3034 return Py_BuildValue("i", WSTOPSIG(status));
3035}
3036#endif /* WSTOPSIG */
3037
3038#endif /* HAVE_SYS_WAIT_H */
3039
3040
Guido van Rossum94f6f721999-01-06 18:42:14 +00003041#if defined(HAVE_FSTATVFS)
3042#include <sys/statvfs.h>
3043
3044static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003045"fstatvfs(fd) -> \n\
3046 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003047Perform an fstatvfs system call on the given fd.";
3048
3049static PyObject *
3050posix_fstatvfs(self, args)
3051 PyObject *self;
3052 PyObject *args;
3053{
3054 int fd, res;
3055 struct statvfs st;
3056 if (!PyArg_ParseTuple(args, "i", &fd))
3057 return NULL;
3058 Py_BEGIN_ALLOW_THREADS
3059 res = fstatvfs(fd, &st);
3060 Py_END_ALLOW_THREADS
3061 if (res != 0)
3062 return posix_error();
3063#if !defined(HAVE_LARGEFILE_SUPPORT)
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) st.f_blocks,
3068 (long) st.f_bfree,
3069 (long) st.f_bavail,
3070 (long) st.f_files,
3071 (long) st.f_ffree,
3072 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003073 (long) st.f_flag,
3074 (long) st.f_namemax);
3075#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003076 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003077 (long) st.f_bsize,
3078 (long) st.f_frsize,
3079 (LONG_LONG) st.f_blocks,
3080 (LONG_LONG) st.f_bfree,
3081 (LONG_LONG) st.f_bavail,
3082 (LONG_LONG) st.f_files,
3083 (LONG_LONG) st.f_ffree,
3084 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003085 (long) st.f_flag,
3086 (long) st.f_namemax);
3087#endif
3088}
3089#endif /* HAVE_FSTATVFS */
3090
3091
3092#if defined(HAVE_STATVFS)
3093#include <sys/statvfs.h>
3094
3095static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003096"statvfs(path) -> \n\
3097 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003098Perform a statvfs system call on the given path.";
3099
3100static PyObject *
3101posix_statvfs(self, args)
3102 PyObject *self;
3103 PyObject *args;
3104{
3105 char *path;
3106 int res;
3107 struct statvfs st;
3108 if (!PyArg_ParseTuple(args, "s", &path))
3109 return NULL;
3110 Py_BEGIN_ALLOW_THREADS
3111 res = statvfs(path, &st);
3112 Py_END_ALLOW_THREADS
3113 if (res != 0)
3114 return posix_error_with_filename(path);
3115#if !defined(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) st.f_blocks,
3120 (long) st.f_bfree,
3121 (long) st.f_bavail,
3122 (long) st.f_files,
3123 (long) st.f_ffree,
3124 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003125 (long) st.f_flag,
3126 (long) st.f_namemax);
3127#else /* HAVE_LARGEFILE_SUPPORT */
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003128 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003129 (long) st.f_bsize,
3130 (long) st.f_frsize,
3131 (LONG_LONG) st.f_blocks,
3132 (LONG_LONG) st.f_bfree,
3133 (LONG_LONG) st.f_bavail,
3134 (LONG_LONG) st.f_files,
3135 (LONG_LONG) st.f_ffree,
3136 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003137 (long) st.f_flag,
3138 (long) st.f_namemax);
3139#endif
3140}
3141#endif /* HAVE_STATVFS */
3142
3143
Barry Warsaw53699e91996-12-10 23:23:01 +00003144static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00003145 {"access", posix_access, 0, posix_access__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003146#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +00003147 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003148#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003149 {"chdir", posix_chdir, 0, posix_chdir__doc__},
3150 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003151#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003152 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003153#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00003154#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003155 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00003156#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00003157#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003158 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003159#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003160 {"listdir", posix_listdir, 0, posix_listdir__doc__},
3161 {"lstat", posix_lstat, 0, posix_lstat__doc__},
3162 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003163#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003164 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003165#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003166#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003167 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003168#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003169 {"rename", posix_rename, 0, posix_rename__doc__},
3170 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
3171 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003172#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003173 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003174#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003175#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003176 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003177#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003178 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003179#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003180 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003181#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003182 {"unlink", posix_unlink, 0, posix_unlink__doc__},
3183 {"remove", posix_unlink, 0, posix_remove__doc__},
3184 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003185#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003186 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003187#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003188 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003189#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003190 {"execv", posix_execv, 0, posix_execv__doc__},
3191 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003192#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00003193#ifdef HAVE_SPAWNV
3194 {"spawnv", posix_spawnv, 0, posix_spawnv__doc__},
3195 {"spawnve", posix_spawnve, 0, posix_spawnve__doc__},
3196#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003197#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003198 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003199#endif /* HAVE_FORK */
3200#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003201 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003202#endif /* HAVE_GETEGID */
3203#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003204 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003205#endif /* HAVE_GETEUID */
3206#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003207 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003208#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003209 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003210#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003211 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003212#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003213#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003214 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003215#endif /* HAVE_GETPPID */
3216#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003217 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003218#endif /* HAVE_GETUID */
3219#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003220 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003221#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00003222#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003223 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00003224#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003225#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003226 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003227#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003228#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003229 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003230#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003231#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003232 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003233#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003234#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003235 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003236#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003237#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003238 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003239#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003240#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003241 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003242#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003243#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003244 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003245#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003246#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003247 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003248#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003249#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003250 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003251#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003252#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003253 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003254#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003255 {"open", posix_open, 1, posix_open__doc__},
3256 {"close", posix_close, 0, posix_close__doc__},
3257 {"dup", posix_dup, 0, posix_dup__doc__},
3258 {"dup2", posix_dup2, 0, posix_dup2__doc__},
3259 {"lseek", posix_lseek, 0, posix_lseek__doc__},
3260 {"read", posix_read, 0, posix_read__doc__},
3261 {"write", posix_write, 0, posix_write__doc__},
3262 {"fstat", posix_fstat, 0, posix_fstat__doc__},
3263 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003264#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003265 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003266#endif
3267#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003268 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003269#endif
3270#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003271 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003272#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003273#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003274 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003275#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00003276#ifdef HAVE_STRERROR
3277 {"strerror", posix_strerror, 1, posix_strerror__doc__},
3278#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00003279#ifdef HAVE_FSYNC
3280 {"fsync", posix_fsync, 0, posix_fsync__doc__},
3281#endif
3282#ifdef HAVE_FDATASYNC
3283 {"fdatasync", posix_fdatasync, 0, posix_fdatasync__doc__},
3284#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00003285#ifdef HAVE_SYS_WAIT_H
3286#ifdef WIFSTOPPED
3287 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
3288#endif /* WIFSTOPPED */
3289#ifdef WIFSIGNALED
3290 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
3291#endif /* WIFSIGNALED */
3292#ifdef WIFEXITED
3293 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
3294#endif /* WIFEXITED */
3295#ifdef WEXITSTATUS
3296 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
3297#endif /* WEXITSTATUS */
3298#ifdef WTERMSIG
3299 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
3300#endif /* WTERMSIG */
3301#ifdef WSTOPSIG
3302 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
3303#endif /* WSTOPSIG */
3304#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003305#ifdef HAVE_FSTATVFS
3306 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
3307#endif
3308#ifdef HAVE_STATVFS
3309 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
3310#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003311 {NULL, NULL} /* Sentinel */
3312};
3313
3314
Barry Warsaw4a342091996-12-19 23:50:02 +00003315static int
3316ins(d, symbol, value)
3317 PyObject* d;
3318 char* symbol;
3319 long value;
3320{
3321 PyObject* v = PyInt_FromLong(value);
3322 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
3323 return -1; /* triggers fatal error */
3324
3325 Py_DECREF(v);
3326 return 0;
3327}
3328
Guido van Rossumd48f2521997-12-05 22:19:34 +00003329#if defined(PYOS_OS2)
3330/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
3331static int insertvalues(PyObject *d)
3332{
3333 APIRET rc;
3334 ULONG values[QSV_MAX+1];
3335 PyObject *v;
3336 char *ver, tmp[10];
3337
3338 Py_BEGIN_ALLOW_THREADS
3339 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
3340 Py_END_ALLOW_THREADS
3341
3342 if (rc != NO_ERROR) {
3343 os2_error(rc);
3344 return -1;
3345 }
3346
3347 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3348 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3349 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3350 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3351 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3352 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3353 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3354
3355 switch (values[QSV_VERSION_MINOR]) {
3356 case 0: ver = "2.00"; break;
3357 case 10: ver = "2.10"; break;
3358 case 11: ver = "2.11"; break;
3359 case 30: ver = "3.00"; break;
3360 case 40: ver = "4.00"; break;
3361 case 50: ver = "5.00"; break;
3362 default:
3363 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3364 values[QSV_VERSION_MINOR]);
3365 ver = &tmp[0];
3366 }
3367
3368 /* Add Indicator of the Version of the Operating System */
3369 v = PyString_FromString(ver);
3370 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3371 return -1;
3372 Py_DECREF(v);
3373
3374 /* Add Indicator of Which Drive was Used to Boot the System */
3375 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3376 tmp[1] = ':';
3377 tmp[2] = '\0';
3378
3379 v = PyString_FromString(tmp);
3380 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3381 return -1;
3382 Py_DECREF(v);
3383
3384 return 0;
3385}
3386#endif
3387
Barry Warsaw4a342091996-12-19 23:50:02 +00003388static int
3389all_ins(d)
3390 PyObject* d;
3391{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003392#ifdef F_OK
3393 if (ins(d, "F_OK", (long)F_OK)) return -1;
3394#endif
3395#ifdef R_OK
3396 if (ins(d, "R_OK", (long)R_OK)) return -1;
3397#endif
3398#ifdef W_OK
3399 if (ins(d, "W_OK", (long)W_OK)) return -1;
3400#endif
3401#ifdef X_OK
3402 if (ins(d, "X_OK", (long)X_OK)) return -1;
3403#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003404#ifdef WNOHANG
3405 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3406#endif
3407#ifdef O_RDONLY
3408 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3409#endif
3410#ifdef O_WRONLY
3411 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3412#endif
3413#ifdef O_RDWR
3414 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3415#endif
3416#ifdef O_NDELAY
3417 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3418#endif
3419#ifdef O_NONBLOCK
3420 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3421#endif
3422#ifdef O_APPEND
3423 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3424#endif
3425#ifdef O_DSYNC
3426 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3427#endif
3428#ifdef O_RSYNC
3429 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3430#endif
3431#ifdef O_SYNC
3432 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3433#endif
3434#ifdef O_NOCTTY
3435 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3436#endif
3437#ifdef O_CREAT
3438 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3439#endif
3440#ifdef O_EXCL
3441 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3442#endif
3443#ifdef O_TRUNC
3444 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3445#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003446#ifdef O_BINARY
3447 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3448#endif
3449#ifdef O_TEXT
3450 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3451#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003452
Guido van Rossum246bc171999-02-01 23:54:31 +00003453#ifdef HAVE_SPAWNV
Guido van Rossum7d385291999-02-16 19:38:04 +00003454 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
3455 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
3456 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
3457 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
3458 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00003459#endif
3460
Guido van Rossumd48f2521997-12-05 22:19:34 +00003461#if defined(PYOS_OS2)
3462 if (insertvalues(d)) return -1;
3463#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003464 return 0;
3465}
3466
3467
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003468#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003469#define INITFUNC initnt
3470#define MODNAME "nt"
3471#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003472#if defined(PYOS_OS2)
3473#define INITFUNC initos2
3474#define MODNAME "os2"
3475#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003476#define INITFUNC initposix
3477#define MODNAME "posix"
3478#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003479#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003480
Guido van Rossum3886bb61998-12-04 18:50:17 +00003481DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003482INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003483{
Barry Warsaw53699e91996-12-10 23:23:01 +00003484 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003485
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003486 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003487 posix_methods,
3488 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003489 (PyObject *)NULL,
3490 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003491 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003492
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003493 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003494 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003495 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003496 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003497 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003498
Barry Warsaw4a342091996-12-19 23:50:02 +00003499 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003500 return;
3501
Barry Warsawca74da41999-02-09 19:31:45 +00003502 PyDict_SetItemString(d, "error", PyExc_OSError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003503}