blob: 2a1efa63a909e219fb1effae4e66b62235f56f24 [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;
293 /* XXX This part ignores errors */
294 for (e = environ; *e != NULL; e++) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000295 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000296 char *p = strchr(*e, '=');
297 if (p == NULL)
298 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000299 v = PyString_FromString(p+1);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000300 if (v == NULL)
301 continue;
302 *p = '\0';
Guido van Rossum9068da41999-07-02 02:54:02 +0000303 if (PyDict_GetItemString(d, *e) == NULL)
304 (void) PyDict_SetItemString(d, *e, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000305 *p = '=';
Barry Warsaw53699e91996-12-10 23:23:01 +0000306 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307 }
Guido van Rossumd48f2521997-12-05 22:19:34 +0000308#if defined(PYOS_OS2)
309 {
310 APIRET rc;
311 char buffer[1024]; /* OS/2 Provides a Documented Max of 1024 Chars */
312
313 rc = DosQueryExtLIBPATH(buffer, BEGIN_LIBPATH);
314 if (rc == NO_ERROR) { /* (not a type, envname is NOT 'BEGIN_LIBPATH') */
315 PyObject *v = PyString_FromString(buffer);
316 PyDict_SetItemString(d, "BEGINLIBPATH", v);
317 Py_DECREF(v);
318 }
319 rc = DosQueryExtLIBPATH(buffer, END_LIBPATH);
320 if (rc == NO_ERROR) { /* (not a typo, envname is NOT 'END_LIBPATH') */
321 PyObject *v = PyString_FromString(buffer);
322 PyDict_SetItemString(d, "ENDLIBPATH", v);
323 Py_DECREF(v);
324 }
325 }
326#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000327 return d;
328}
329
330
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000331/* Set a POSIX-specific error from errno, and return NULL */
332
Barry Warsawd58d7641998-07-23 16:14:40 +0000333static PyObject *
334posix_error()
Guido van Rossumad0ee831995-03-01 10:34:45 +0000335{
Barry Warsawca74da41999-02-09 19:31:45 +0000336 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000337}
Barry Warsawd58d7641998-07-23 16:14:40 +0000338static PyObject *
339posix_error_with_filename(name)
340 char* name;
341{
Barry Warsawca74da41999-02-09 19:31:45 +0000342 return PyErr_SetFromErrnoWithFilename(PyExc_OSError, name);
Barry Warsawd58d7641998-07-23 16:14:40 +0000343}
344
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345
Guido van Rossumd48f2521997-12-05 22:19:34 +0000346#if defined(PYOS_OS2)
347/**********************************************************************
348 * Helper Function to Trim and Format OS/2 Messages
349 **********************************************************************/
350 static void
351os2_formatmsg(char *msgbuf, int msglen, char *reason)
352{
353 msgbuf[msglen] = '\0'; /* OS/2 Doesn't Guarantee a Terminator */
354
355 if (strlen(msgbuf) > 0) { /* If Non-Empty Msg, Trim CRLF */
356 char *lastc = &msgbuf[ strlen(msgbuf)-1 ];
357
358 while (lastc > msgbuf && isspace(*lastc))
359 *lastc-- = '\0'; /* Trim Trailing Whitespace (CRLF) */
360 }
361
362 /* Add Optional Reason Text */
363 if (reason) {
364 strcat(msgbuf, " : ");
365 strcat(msgbuf, reason);
366 }
367}
368
369/**********************************************************************
370 * Decode an OS/2 Operating System Error Code
371 *
372 * A convenience function to lookup an OS/2 error code and return a
373 * text message we can use to raise a Python exception.
374 *
375 * Notes:
376 * The messages for errors returned from the OS/2 kernel reside in
377 * the file OSO001.MSG in the \OS2 directory hierarchy.
378 *
379 **********************************************************************/
380 static char *
381os2_strerror(char *msgbuf, int msgbuflen, int errorcode, char *reason)
382{
383 APIRET rc;
384 ULONG msglen;
385
386 /* Retrieve Kernel-Related Error Message from OSO001.MSG File */
387 Py_BEGIN_ALLOW_THREADS
388 rc = DosGetMessage(NULL, 0, msgbuf, msgbuflen,
389 errorcode, "oso001.msg", &msglen);
390 Py_END_ALLOW_THREADS
391
392 if (rc == NO_ERROR)
393 os2_formatmsg(msgbuf, msglen, reason);
394 else
395 sprintf(msgbuf, "unknown OS error #%d", errorcode);
396
397 return msgbuf;
398}
399
400/* Set an OS/2-specific error and return NULL. OS/2 kernel
401 errors are not in a global variable e.g. 'errno' nor are
402 they congruent with posix error numbers. */
403
404static PyObject * os2_error(int code)
405{
406 char text[1024];
407 PyObject *v;
408
409 os2_strerror(text, sizeof(text), code, "");
410
411 v = Py_BuildValue("(is)", code, text);
412 if (v != NULL) {
Barry Warsawca74da41999-02-09 19:31:45 +0000413 PyErr_SetObject(PyExc_OSError, v);
Guido van Rossumd48f2521997-12-05 22:19:34 +0000414 Py_DECREF(v);
415 }
416 return NULL; /* Signal to Python that an Exception is Pending */
417}
418
419#endif /* OS2 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000420
421/* POSIX generic methods */
422
Barry Warsaw53699e91996-12-10 23:23:01 +0000423static PyObject *
Guido van Rossum21142a01999-01-08 21:05:37 +0000424posix_int(args, func)
425 PyObject *args;
426 int (*func) Py_FPROTO((int));
427{
428 int fd;
429 int res;
430 if (!PyArg_Parse(args, "i", &fd))
431 return NULL;
432 Py_BEGIN_ALLOW_THREADS
433 res = (*func)(fd);
434 Py_END_ALLOW_THREADS
435 if (res < 0)
436 return posix_error();
437 Py_INCREF(Py_None);
438 return Py_None;
439}
440
441
442static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000443posix_1str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000444 PyObject *args;
445 int (*func) Py_FPROTO((const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000447 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000448 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000449 if (!PyArg_Parse(args, "s", &path1))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000450 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000451 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000452 res = (*func)(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000453 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000454 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000455 return posix_error_with_filename(path1);
Barry Warsaw53699e91996-12-10 23:23:01 +0000456 Py_INCREF(Py_None);
457 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458}
459
Barry Warsaw53699e91996-12-10 23:23:01 +0000460static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000461posix_2str(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000462 PyObject *args;
463 int (*func) Py_FPROTO((const char *, const char *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000464{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000465 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000466 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000467 if (!PyArg_Parse(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000468 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000469 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000470 res = (*func)(path1, path2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000471 Py_END_ALLOW_THREADS
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000472 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000473 /* XXX how to report both path1 and path2??? */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000475 Py_INCREF(Py_None);
476 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477}
478
Barry Warsaw53699e91996-12-10 23:23:01 +0000479static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480posix_strint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000481 PyObject *args;
482 int (*func) Py_FPROTO((const char *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000484 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000485 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000486 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000487 if (!PyArg_Parse(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000488 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000489 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000490 res = (*func)(path, i);
Barry Warsaw53699e91996-12-10 23:23:01 +0000491 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000492 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000493 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000494 Py_INCREF(Py_None);
495 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496}
497
Barry Warsaw53699e91996-12-10 23:23:01 +0000498static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000499posix_strintint(args, func)
Barry Warsaw53699e91996-12-10 23:23:01 +0000500 PyObject *args;
501 int (*func) Py_FPROTO((const char *, int, int));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000502{
503 char *path;
504 int i,i2;
505 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000506 if (!PyArg_Parse(args, "(sii)", &path, &i, &i2))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000507 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000508 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000509 res = (*func)(path, i, i2);
Barry Warsaw53699e91996-12-10 23:23:01 +0000510 Py_END_ALLOW_THREADS
Guido van Rossumb6775db1994-08-01 11:34:53 +0000511 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000512 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +0000513 Py_INCREF(Py_None);
514 return Py_None;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000515}
516
Barry Warsaw53699e91996-12-10 23:23:01 +0000517static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000518posix_do_stat(self, args, statfunc)
Barry Warsaw53699e91996-12-10 23:23:01 +0000519 PyObject *self;
520 PyObject *args;
521 int (*statfunc) Py_FPROTO((const char *, struct stat *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000522{
523 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000524 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000525 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000526 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000527 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000528 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000529 res = (*statfunc)(path, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +0000530 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000531 if (res != 0)
Barry Warsawd58d7641998-07-23 16:14:40 +0000532 return posix_error_with_filename(path);
Guido van Rossum94f6f721999-01-06 18:42:14 +0000533#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +0000534 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +0000535 (long)st.st_mode,
536 (long)st.st_ino,
537 (long)st.st_dev,
538 (long)st.st_nlink,
539 (long)st.st_uid,
540 (long)st.st_gid,
541 (long)st.st_size,
542 (long)st.st_atime,
543 (long)st.st_mtime,
544 (long)st.st_ctime);
545#else
546 return Py_BuildValue("(lLllllLlll)",
547 (long)st.st_mode,
548 (LONG_LONG)st.st_ino,
549 (long)st.st_dev,
550 (long)st.st_nlink,
551 (long)st.st_uid,
552 (long)st.st_gid,
553 (LONG_LONG)st.st_size,
554 (long)st.st_atime,
555 (long)st.st_mtime,
556 (long)st.st_ctime);
557#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000558}
559
560
561/* POSIX methods */
562
Guido van Rossum94f6f721999-01-06 18:42:14 +0000563static char posix_access__doc__[] =
Guido van Rossum015f22a1999-01-06 22:52:38 +0000564"access(path, mode) -> 1 if granted, 0 otherwise\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000565Test for access to a file.";
566
567static PyObject *
568posix_access(self, args)
569 PyObject *self;
570 PyObject *args;
571{
Guido van Rossum015f22a1999-01-06 22:52:38 +0000572 char *path;
573 int mode;
574 int res;
575
576 if (!PyArg_Parse(args, "(si)", &path, &mode))
577 return NULL;
578 Py_BEGIN_ALLOW_THREADS
579 res = access(path, mode);
580 Py_END_ALLOW_THREADS
581 return(PyInt_FromLong(res == 0 ? 1L : 0L));
Guido van Rossum94f6f721999-01-06 18:42:14 +0000582}
583
Guido van Rossumd371ff11999-01-25 16:12:23 +0000584#ifndef F_OK
585#define F_OK 0
586#endif
587#ifndef R_OK
588#define R_OK 4
589#endif
590#ifndef W_OK
591#define W_OK 2
592#endif
593#ifndef X_OK
594#define X_OK 1
595#endif
596
597#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +0000598static char posix_ttyname__doc__[] =
Guido van Rossum61eeb041999-02-22 15:29:15 +0000599"ttyname(fd) -> String\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +0000600Return the name of the terminal device connected to 'fd'.";
601
602static PyObject *
603posix_ttyname(self, args)
604 PyObject *self;
605 PyObject *args;
606{
607 PyObject *file;
608 int id;
609 char *ret;
610
Guido van Rossum94f6f721999-01-06 18:42:14 +0000611 if (!PyArg_Parse(args, "i", &id))
612 return NULL;
613
Guido van Rossum94f6f721999-01-06 18:42:14 +0000614 ret = ttyname(id);
615 if (ret == NULL)
616 return(posix_error());
617 return(PyString_FromString(ret));
618}
Guido van Rossumd371ff11999-01-25 16:12:23 +0000619#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +0000620
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000621static char posix_chdir__doc__[] =
622"chdir(path) -> None\n\
623Change the current working directory to the specified path.";
624
Barry Warsaw53699e91996-12-10 23:23:01 +0000625static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000626posix_chdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000627 PyObject *self;
628 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000629{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000630 return posix_1str(args, chdir);
631}
632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000633
634static char posix_chmod__doc__[] =
635"chmod(path, mode) -> None\n\
636Change the access permissions of a file.";
637
Barry Warsaw53699e91996-12-10 23:23:01 +0000638static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000639posix_chmod(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000640 PyObject *self;
641 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000642{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000643 return posix_strint(args, chmod);
644}
645
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000646
Guido van Rossum21142a01999-01-08 21:05:37 +0000647#ifdef HAVE_FSYNC
648static char posix_fsync__doc__[] =
649"fsync(fildes) -> None\n\
650force write of file with filedescriptor to disk.";
651
652static PyObject *
653posix_fsync(self, args)
654 PyObject *self;
655 PyObject *args;
656{
Guido van Rossum21142a01999-01-08 21:05:37 +0000657 return posix_int(args, fsync);
658}
659#endif /* HAVE_FSYNC */
660
661#ifdef HAVE_FDATASYNC
662static char posix_fdatasync__doc__[] =
663"fdatasync(fildes) -> None\n\
664force write of file with filedescriptor to disk.\n\
665 does not force update of metadata.";
666
Guido van Rossum5d00b6d1999-01-08 21:28:05 +0000667extern int fdatasync(int); /* Prototype just in case */
668
Guido van Rossum21142a01999-01-08 21:05:37 +0000669static PyObject *
670posix_fdatasync(self, args)
671 PyObject *self;
672 PyObject *args;
673{
Guido van Rossum21142a01999-01-08 21:05:37 +0000674 return posix_int(args, fdatasync);
675}
676#endif /* HAVE_FDATASYNC */
677
678
Guido van Rossumb6775db1994-08-01 11:34:53 +0000679#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000680static char posix_chown__doc__[] =
681"chown(path, uid, gid) -> None\n\
682Change the owner and group id of path to the numeric uid and gid.";
683
Barry Warsaw53699e91996-12-10 23:23:01 +0000684static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000685posix_chown(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000686 PyObject *self;
687 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000688{
689 return posix_strintint(args, chown);
690}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000691#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000692
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000693
Guido van Rossum36bc6801995-06-14 22:54:23 +0000694#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000695static char posix_getcwd__doc__[] =
696"getcwd() -> path\n\
697Return a string representing the current working directory.";
698
Barry Warsaw53699e91996-12-10 23:23:01 +0000699static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000700posix_getcwd(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000701 PyObject *self;
702 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703{
704 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000705 char *res;
Barry Warsaw53699e91996-12-10 23:23:01 +0000706 if (!PyArg_NoArgs(args))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000707 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000708 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000709 res = getcwd(buf, sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +0000710 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000711 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000712 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +0000713 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714}
Guido van Rossum36bc6801995-06-14 22:54:23 +0000715#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000717
Guido van Rossumb6775db1994-08-01 11:34:53 +0000718#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000719static char posix_link__doc__[] =
720"link(src, dst) -> None\n\
721Create a hard link to a file.";
722
Barry Warsaw53699e91996-12-10 23:23:01 +0000723static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000724posix_link(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000725 PyObject *self;
726 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000727{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000728 return posix_2str(args, link);
729}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000730#endif /* HAVE_LINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000731
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000732
733static char posix_listdir__doc__[] =
734"listdir(path) -> list_of_strings\n\
735Return a list containing the names of the entries in the directory.\n\
736\n\
737 path: path of directory to list\n\
738\n\
739The list is in arbitrary order. It does not include the special\n\
740entries '.' and '..' even if they are present in the directory.";
741
Barry Warsaw53699e91996-12-10 23:23:01 +0000742static PyObject *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000743posix_listdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000744 PyObject *self;
745 PyObject *args;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000746{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000747 /* XXX Should redo this putting the (now four) versions of opendir
Guido van Rossum6d8841c1997-08-14 19:57:39 +0000748 in separate files instead of having them all here... */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000749#if defined(MS_WIN32) && !defined(HAVE_OPENDIR)
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000750
Guido van Rossumb6775db1994-08-01 11:34:53 +0000751 char *name;
752 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000753 PyObject *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000754 HANDLE hFindFile;
755 WIN32_FIND_DATA FileData;
756 char namebuf[MAX_PATH+5];
757
Guido van Rossum7e488981998-10-08 02:25:24 +0000758 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossumb6775db1994-08-01 11:34:53 +0000759 return NULL;
760 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000761 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossumb6775db1994-08-01 11:34:53 +0000762 return NULL;
763 }
764 strcpy(namebuf, name);
765 if (namebuf[len-1] != '/' && namebuf[len-1] != '\\')
766 namebuf[len++] = '/';
767 strcpy(namebuf + len, "*.*");
768
Barry Warsaw53699e91996-12-10 23:23:01 +0000769 if ((d = PyList_New(0)) == NULL)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000770 return NULL;
771
772 hFindFile = FindFirstFile(namebuf, &FileData);
773 if (hFindFile == INVALID_HANDLE_VALUE) {
774 errno = GetLastError();
Guido van Rossum617bc191998-08-06 03:23:32 +0000775 if (errno == ERROR_FILE_NOT_FOUND)
776 return PyList_New(0);
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000777 return posix_error_with_filename(name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000778 }
779 do {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000780 if (FileData.cFileName[0] == '.' &&
781 (FileData.cFileName[1] == '\0' ||
782 FileData.cFileName[1] == '.' &&
783 FileData.cFileName[2] == '\0'))
784 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000785 v = PyString_FromString(FileData.cFileName);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000786 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000787 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000788 d = NULL;
789 break;
790 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000791 if (PyList_Append(d, v) != 0) {
792 Py_DECREF(v);
793 Py_DECREF(d);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000794 d = NULL;
795 break;
796 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000797 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000798 } while (FindNextFile(hFindFile, &FileData) == TRUE);
799
800 if (FindClose(hFindFile) == FALSE) {
801 errno = GetLastError();
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000802 return posix_error_with_filename(&name);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000803 }
804
805 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000806
Guido van Rossum8d665e61996-06-26 18:22:49 +0000807#else /* !MS_WIN32 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000808#ifdef _MSC_VER /* 16-bit Windows */
809
810#ifndef MAX_PATH
811#define MAX_PATH 250
812#endif
813 char *name, *pt;
814 int len;
Barry Warsaw53699e91996-12-10 23:23:01 +0000815 PyObject *d, *v;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000816 char namebuf[MAX_PATH+5];
817 struct _find_t ep;
818
Guido van Rossum7e488981998-10-08 02:25:24 +0000819 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000820 return NULL;
821 if (len >= MAX_PATH) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000822 PyErr_SetString(PyExc_ValueError, "path too long");
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000823 return NULL;
824 }
825 strcpy(namebuf, name);
826 for (pt = namebuf; *pt; pt++)
827 if (*pt == '/')
828 *pt = '\\';
829 if (namebuf[len-1] != '\\')
830 namebuf[len++] = '\\';
831 strcpy(namebuf + len, "*.*");
832
Barry Warsaw53699e91996-12-10 23:23:01 +0000833 if ((d = PyList_New(0)) == NULL)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000834 return NULL;
835
836 if (_dos_findfirst(namebuf, _A_RDONLY |
Barry Warsaw43d68b81996-12-19 22:10:44 +0000837 _A_HIDDEN | _A_SYSTEM | _A_SUBDIR, &ep) != 0)
838 {
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000839 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000840 return posix_error_with_filename(name);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000841 }
842 do {
843 if (ep.name[0] == '.' &&
844 (ep.name[1] == '\0' ||
845 ep.name[1] == '.' &&
846 ep.name[2] == '\0'))
847 continue;
848 strcpy(namebuf, ep.name);
849 for (pt = namebuf; *pt; pt++)
850 if (isupper(*pt))
851 *pt = tolower(*pt);
Barry Warsaw53699e91996-12-10 23:23:01 +0000852 v = PyString_FromString(namebuf);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000853 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000854 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000855 d = NULL;
856 break;
857 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000858 if (PyList_Append(d, v) != 0) {
859 Py_DECREF(v);
860 Py_DECREF(d);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000861 d = NULL;
862 break;
863 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000864 Py_DECREF(v);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000865 } while (_dos_findnext(&ep) == 0);
866
867 return d;
868
869#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000870#if defined(PYOS_OS2)
871
872#ifndef MAX_PATH
873#define MAX_PATH CCHMAXPATH
874#endif
875 char *name, *pt;
876 int len;
877 PyObject *d, *v;
878 char namebuf[MAX_PATH+5];
879 HDIR hdir = 1;
880 ULONG srchcnt = 1;
881 FILEFINDBUF3 ep;
882 APIRET rc;
883
Guido van Rossum7e488981998-10-08 02:25:24 +0000884 if (!PyArg_Parse(args, "t#", &name, &len))
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000885 return NULL;
886 if (len >= MAX_PATH) {
887 PyErr_SetString(PyExc_ValueError, "path too long");
888 return NULL;
889 }
890 strcpy(namebuf, name);
891 for (pt = namebuf; *pt; pt++)
892 if (*pt == '/')
893 *pt = '\\';
894 if (namebuf[len-1] != '\\')
895 namebuf[len++] = '\\';
896 strcpy(namebuf + len, "*.*");
897
898 if ((d = PyList_New(0)) == NULL)
899 return NULL;
900
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000901 rc = DosFindFirst(namebuf, /* Wildcard Pattern to Match */
902 &hdir, /* Handle to Use While Search Directory */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000903 FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM | FILE_DIRECTORY,
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000904 &ep, sizeof(ep), /* Structure to Receive Directory Entry */
905 &srchcnt, /* Max and Actual Count of Entries Per Iteration */
906 FIL_STANDARD); /* Format of Entry (EAs or Not) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000907
908 if (rc != NO_ERROR) {
909 errno = ENOENT;
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000910 return posix_error_with_filename(name);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000911 }
912
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000913 if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000914 do {
915 if (ep.achName[0] == '.'
916 && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000917 continue; /* Skip Over "." and ".." Names */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000918
919 strcpy(namebuf, ep.achName);
920
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000921 /* Leave Case of Name Alone -- In Native Form */
922 /* (Removed Forced Lowercasing Code) */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000923
924 v = PyString_FromString(namebuf);
925 if (v == NULL) {
926 Py_DECREF(d);
927 d = NULL;
928 break;
929 }
930 if (PyList_Append(d, v) != 0) {
931 Py_DECREF(v);
932 Py_DECREF(d);
933 d = NULL;
934 break;
935 }
936 Py_DECREF(v);
937 } while (DosFindNext(hdir, &ep, sizeof(ep), &srchcnt) == NO_ERROR && srchcnt > 0);
938 }
939
940 return d;
941#else
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000942
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000943 char *name;
Barry Warsaw53699e91996-12-10 23:23:01 +0000944 PyObject *d, *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000945 DIR *dirp;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000946 struct dirent *ep;
Barry Warsaw53699e91996-12-10 23:23:01 +0000947 if (!PyArg_Parse(args, "s", &name))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000948 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +0000949 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000950 if ((dirp = opendir(name)) == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000951 Py_BLOCK_THREADS
Barry Warsawf63b8cc1999-05-27 23:13:21 +0000952 return posix_error_with_filename(name);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000953 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000954 if ((d = PyList_New(0)) == NULL) {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000955 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000956 Py_BLOCK_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000957 return NULL;
958 }
959 while ((ep = readdir(dirp)) != NULL) {
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000960 if (ep->d_name[0] == '.' &&
961 (NAMLEN(ep) == 1 ||
Guido van Rossuma376cc51996-12-05 23:43:35 +0000962 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
Guido van Rossum24f42ac1995-07-18 18:16:52 +0000963 continue;
Barry Warsaw53699e91996-12-10 23:23:01 +0000964 v = PyString_FromStringAndSize(ep->d_name, NAMLEN(ep));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000965 if (v == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +0000966 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000967 d = NULL;
968 break;
969 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000970 if (PyList_Append(d, v) != 0) {
971 Py_DECREF(v);
972 Py_DECREF(d);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000973 d = NULL;
974 break;
975 }
Barry Warsaw53699e91996-12-10 23:23:01 +0000976 Py_DECREF(v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000977 }
978 closedir(dirp);
Barry Warsaw53699e91996-12-10 23:23:01 +0000979 Py_END_ALLOW_THREADS
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000980
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000981 return d;
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000982
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000983#endif /* !PYOS_OS2 */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000984#endif /* !_MSC_VER */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000985#endif /* !MS_WIN32 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000986}
987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000988static char posix_mkdir__doc__[] =
989"mkdir(path [, mode=0777]) -> None\n\
990Create a directory.";
991
Barry Warsaw53699e91996-12-10 23:23:01 +0000992static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000993posix_mkdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +0000994 PyObject *self;
995 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000996{
Guido van Rossumb0824db1996-02-25 04:50:32 +0000997 int res;
998 char *path;
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000999 int mode = 0777;
Barry Warsaw53699e91996-12-10 23:23:01 +00001000 if (!PyArg_ParseTuple(args, "s|i", &path, &mode))
Guido van Rossumb0824db1996-02-25 04:50:32 +00001001 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001002 Py_BEGIN_ALLOW_THREADS
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001003#if ( defined(__WATCOMC__) || defined(_MSC_VER) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001004 res = mkdir(path);
1005#else
Guido van Rossumb0824db1996-02-25 04:50:32 +00001006 res = mkdir(path, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001007#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001008 Py_END_ALLOW_THREADS
Guido van Rossumb0824db1996-02-25 04:50:32 +00001009 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001010 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001011 Py_INCREF(Py_None);
1012 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001013}
1014
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001015
Guido van Rossumb6775db1994-08-01 11:34:53 +00001016#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001017static char posix_nice__doc__[] =
1018"nice(inc) -> new_priority\n\
1019Decrease the priority of process and return new priority.";
1020
Barry Warsaw53699e91996-12-10 23:23:01 +00001021static PyObject *
Guido van Rossum775f4da1993-01-09 17:18:52 +00001022posix_nice(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001023 PyObject *self;
1024 PyObject *args;
Guido van Rossum775f4da1993-01-09 17:18:52 +00001025{
1026 int increment, value;
1027
Barry Warsaw53699e91996-12-10 23:23:01 +00001028 if (!PyArg_Parse(args, "i", &increment))
Guido van Rossum775f4da1993-01-09 17:18:52 +00001029 return NULL;
1030 value = nice(increment);
1031 if (value == -1)
1032 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001033 return PyInt_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00001034}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001035#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001036
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001037
1038static char posix_rename__doc__[] =
1039"rename(old, new) -> None\n\
1040Rename a file or directory.";
1041
Barry Warsaw53699e91996-12-10 23:23:01 +00001042static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001043posix_rename(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001044 PyObject *self;
1045 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001046{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001047 return posix_2str(args, rename);
1048}
1049
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001050
1051static char posix_rmdir__doc__[] =
1052"rmdir(path) -> None\n\
1053Remove a directory.";
1054
Barry Warsaw53699e91996-12-10 23:23:01 +00001055static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001056posix_rmdir(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001057 PyObject *self;
1058 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001059{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001060 return posix_1str(args, rmdir);
1061}
1062
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001063
1064static char posix_stat__doc__[] =
1065"stat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
1066Perform a stat system call on the given path.";
1067
Barry Warsaw53699e91996-12-10 23:23:01 +00001068static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001069posix_stat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001070 PyObject *self;
1071 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001072{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001073 return posix_do_stat(self, args, stat);
1074}
1075
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001076
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001077#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001078static char posix_system__doc__[] =
1079"system(command) -> exit_status\n\
1080Execute the command (a string) in a subshell.";
1081
Barry Warsaw53699e91996-12-10 23:23:01 +00001082static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001083posix_system(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001084 PyObject *self;
1085 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001086{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001087 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001088 long sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001089 if (!PyArg_Parse(args, "s", &command))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001090 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001091 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001092 sts = system(command);
Barry Warsaw53699e91996-12-10 23:23:01 +00001093 Py_END_ALLOW_THREADS
1094 return PyInt_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001095}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001096#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001097
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001098
1099static char posix_umask__doc__[] =
1100"umask(new_mask) -> old_mask\n\
1101Set the current numeric umask and return the previous umask.";
1102
Barry Warsaw53699e91996-12-10 23:23:01 +00001103static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001104posix_umask(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001105 PyObject *self;
1106 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001107{
1108 int i;
Barry Warsaw53699e91996-12-10 23:23:01 +00001109 if (!PyArg_Parse(args, "i", &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001110 return NULL;
1111 i = umask(i);
1112 if (i < 0)
1113 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001114 return PyInt_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001115}
1116
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001117
1118static char posix_unlink__doc__[] =
1119"unlink(path) -> None\n\
1120Remove a file (same as remove(path)).";
1121
1122static char posix_remove__doc__[] =
1123"remove(path) -> None\n\
1124Remove a file (same as unlink(path)).";
1125
Barry Warsaw53699e91996-12-10 23:23:01 +00001126static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001127posix_unlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001128 PyObject *self;
1129 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001130{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001131 return posix_1str(args, unlink);
1132}
1133
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001134
Guido van Rossumb6775db1994-08-01 11:34:53 +00001135#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001136static char posix_uname__doc__[] =
1137"uname() -> (sysname, nodename, release, version, machine)\n\
1138Return a tuple identifying the current operating system.";
1139
Barry Warsaw53699e91996-12-10 23:23:01 +00001140static PyObject *
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001141posix_uname(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001142 PyObject *self;
1143 PyObject *args;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001144{
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001145 struct utsname u;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001146 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00001147 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001148 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001149 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001150 res = uname(&u);
Barry Warsaw53699e91996-12-10 23:23:01 +00001151 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001152 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001153 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001154 return Py_BuildValue("(sssss)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00001155 u.sysname,
1156 u.nodename,
1157 u.release,
1158 u.version,
1159 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001160}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001161#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001162
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001163
1164static char posix_utime__doc__[] =
1165"utime(path, (atime, utime)) -> None\n\
1166Set the access and modified time of the file to the given values.";
1167
Barry Warsaw53699e91996-12-10 23:23:01 +00001168static PyObject *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001169posix_utime(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001170 PyObject *self;
1171 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001173 char *path;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001174 long atime, mtime;
Guido van Rossumff4949e1992-08-05 19:58:53 +00001175 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001176
Guido van Rossum6d8841c1997-08-14 19:57:39 +00001177/* XXX should define struct utimbuf instead, above */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001178#ifdef HAVE_UTIME_H
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001179 struct utimbuf buf;
1180#define ATIME buf.actime
1181#define MTIME buf.modtime
1182#define UTIME_ARG &buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001183#else /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001184 time_t buf[2];
1185#define ATIME buf[0]
1186#define MTIME buf[1]
1187#define UTIME_ARG buf
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001188#endif /* HAVE_UTIME_H */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001189
Barry Warsaw53699e91996-12-10 23:23:01 +00001190 if (!PyArg_Parse(args, "(s(ll))", &path, &atime, &mtime))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001191 return NULL;
Guido van Rossumf8803dd1995-01-26 00:37:45 +00001192 ATIME = atime;
Guido van Rossumd1b34811995-02-07 15:39:29 +00001193 MTIME = mtime;
Barry Warsaw53699e91996-12-10 23:23:01 +00001194 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001195 res = utime(path, UTIME_ARG);
Barry Warsaw53699e91996-12-10 23:23:01 +00001196 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +00001197 if (res < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00001198 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00001199 Py_INCREF(Py_None);
1200 return Py_None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001201#undef UTIME_ARG
1202#undef ATIME
1203#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001204}
1205
Guido van Rossum85e3b011991-06-03 12:42:10 +00001206
Guido van Rossum3b066191991-06-04 19:40:25 +00001207/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001208
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001209static char posix__exit__doc__[] =
1210"_exit(status)\n\
1211Exit to the system with specified status, without normal exit processing.";
1212
Barry Warsaw53699e91996-12-10 23:23:01 +00001213static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001214posix__exit(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001215 PyObject *self;
1216 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001217{
1218 int sts;
Barry Warsaw53699e91996-12-10 23:23:01 +00001219 if (!PyArg_Parse(args, "i", &sts))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001220 return NULL;
1221 _exit(sts);
Guido van Rossuma376cc51996-12-05 23:43:35 +00001222 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00001223}
1224
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001225
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001226#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001227static char posix_execv__doc__[] =
1228"execv(path, args)\n\
1229Execute an executable path with arguments, replacing current process.\n\
1230\n\
1231 path: path of executable file\n\
1232 args: tuple or list of strings";
1233
Barry Warsaw53699e91996-12-10 23:23:01 +00001234static PyObject *
Guido van Rossum89b33251993-10-22 14:26:06 +00001235posix_execv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001236 PyObject *self;
1237 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001238{
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001239 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001240 PyObject *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001241 char **argvlist;
1242 int i, argc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001243 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossum85e3b011991-06-03 12:42:10 +00001244
Guido van Rossum89b33251993-10-22 14:26:06 +00001245 /* execv has two arguments: (path, argv), where
Guido van Rossum85e3b011991-06-03 12:42:10 +00001246 argv is a list or tuple of strings. */
1247
Barry Warsaw53699e91996-12-10 23:23:01 +00001248 if (!PyArg_Parse(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001249 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001250 if (PyList_Check(argv)) {
1251 argc = PyList_Size(argv);
1252 getitem = PyList_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001253 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001254 else if (PyTuple_Check(argv)) {
1255 argc = PyTuple_Size(argv);
1256 getitem = PyTuple_GetItem;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001257 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001258 else {
1259 badarg:
Barry Warsaw53699e91996-12-10 23:23:01 +00001260 PyErr_BadArgument();
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001261 return NULL;
1262 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001263
Barry Warsaw53699e91996-12-10 23:23:01 +00001264 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001265 if (argvlist == NULL)
1266 return NULL;
1267 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001268 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1269 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001270 goto badarg;
1271 }
Guido van Rossum85e3b011991-06-03 12:42:10 +00001272 }
1273 argvlist[argc] = NULL;
1274
Guido van Rossumb6775db1994-08-01 11:34:53 +00001275#ifdef BAD_EXEC_PROTOTYPES
1276 execv(path, (const char **) argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001277#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001278 execv(path, argvlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001279#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001280
Guido van Rossum85e3b011991-06-03 12:42:10 +00001281 /* If we get here it's definitely an error */
1282
Barry Warsaw53699e91996-12-10 23:23:01 +00001283 PyMem_DEL(argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001284 return posix_error();
1285}
1286
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001287
1288static char posix_execve__doc__[] =
1289"execve(path, args, env)\n\
1290Execute a path with arguments and environment, replacing current process.\n\
1291\n\
1292 path: path of executable file\n\
1293 args: tuple or list of arguments\n\
1294 env: dictonary of strings mapping to strings";
1295
Barry Warsaw53699e91996-12-10 23:23:01 +00001296static PyObject *
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001297posix_execve(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001298 PyObject *self;
1299 PyObject *args;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001300{
1301 char *path;
Barry Warsaw53699e91996-12-10 23:23:01 +00001302 PyObject *argv, *env;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001303 char **argvlist;
1304 char **envlist;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001305 PyObject *key, *val, *keys=NULL, *vals=NULL;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001306 int i, pos, argc, envc;
Barry Warsaw53699e91996-12-10 23:23:01 +00001307 PyObject *(*getitem) Py_PROTO((PyObject *, int));
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001308
1309 /* execve has three arguments: (path, argv, env), where
1310 argv is a list or tuple of strings and env is a dictionary
1311 like posix.environ. */
1312
Barry Warsaw53699e91996-12-10 23:23:01 +00001313 if (!PyArg_Parse(args, "(sOO)", &path, &argv, &env))
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001314 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001315 if (PyList_Check(argv)) {
1316 argc = PyList_Size(argv);
1317 getitem = PyList_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001318 }
Barry Warsaw53699e91996-12-10 23:23:01 +00001319 else if (PyTuple_Check(argv)) {
1320 argc = PyTuple_Size(argv);
1321 getitem = PyTuple_GetItem;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001322 }
1323 else {
Barry Warsaw53699e91996-12-10 23:23:01 +00001324 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001325 return NULL;
1326 }
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001327 if (!PyMapping_Check(env)) {
1328 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001329 return NULL;
1330 }
1331
Barry Warsaw53699e91996-12-10 23:23:01 +00001332 argvlist = PyMem_NEW(char *, argc+1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001333 if (argvlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001334 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001335 return NULL;
1336 }
1337 for (i = 0; i < argc; i++) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001338 if (!PyArg_Parse((*getitem)(argv, i),
Barry Warsaw43d68b81996-12-19 22:10:44 +00001339 "s;argv must be list of strings",
1340 &argvlist[i]))
1341 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001342 goto fail_1;
1343 }
1344 }
1345 argvlist[argc] = NULL;
1346
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001347 i = PyMapping_Length(env);
Barry Warsaw53699e91996-12-10 23:23:01 +00001348 envlist = PyMem_NEW(char *, i + 1);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001349 if (envlist == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001350 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001351 goto fail_1;
1352 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001353 envc = 0;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001354 keys = PyMapping_Keys(env);
1355 vals = PyMapping_Values(env);
1356 if (!keys || !vals)
1357 goto fail_2;
1358
1359 for (pos = 0; pos < i; pos++) {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001360 char *p, *k, *v;
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001361
1362 key = PyList_GetItem(keys, pos);
1363 val = PyList_GetItem(vals, pos);
1364 if (!key || !val)
1365 goto fail_2;
1366
Barry Warsaw53699e91996-12-10 23:23:01 +00001367 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
Barry Warsaw43d68b81996-12-19 22:10:44 +00001368 !PyArg_Parse(val, "s;non-string value in env", &v))
1369 {
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001370 goto fail_2;
1371 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00001372
1373#if defined(PYOS_OS2)
1374 /* Omit Pseudo-Env Vars that Would Confuse Programs if Passed On */
1375 if (stricmp(k, "BEGINLIBPATH") != 0 && stricmp(k, "ENDLIBPATH") != 0) {
1376#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001377 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001378 if (p == NULL) {
Barry Warsaw53699e91996-12-10 23:23:01 +00001379 PyErr_NoMemory();
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001380 goto fail_2;
1381 }
1382 sprintf(p, "%s=%s", k, v);
1383 envlist[envc++] = p;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001384#if defined(PYOS_OS2)
1385 }
1386#endif
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001387 }
1388 envlist[envc] = 0;
1389
Guido van Rossumb6775db1994-08-01 11:34:53 +00001390
1391#ifdef BAD_EXEC_PROTOTYPES
1392 execve(path, (const char **)argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001393#else /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001394 execve(path, argvlist, envlist);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001395#endif /* BAD_EXEC_PROTOTYPES */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001396
1397 /* If we get here it's definitely an error */
1398
1399 (void) posix_error();
1400
1401 fail_2:
1402 while (--envc >= 0)
Barry Warsaw53699e91996-12-10 23:23:01 +00001403 PyMem_DEL(envlist[envc]);
1404 PyMem_DEL(envlist);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001405 fail_1:
Barry Warsaw53699e91996-12-10 23:23:01 +00001406 PyMem_DEL(argvlist);
Barry Warsaw5ed19dc1997-01-29 15:08:24 +00001407 Py_XDECREF(vals);
1408 Py_XDECREF(keys);
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001409 return NULL;
1410}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001411#endif /* HAVE_EXECV */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00001412
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001413
Guido van Rossuma1065681999-01-25 23:20:23 +00001414#ifdef HAVE_SPAWNV
1415static char posix_spawnv__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001416"spawnv(mode, path, args)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001417Execute an executable path with arguments, replacing current process.\n\
1418\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001419 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001420 path: path of executable file\n\
1421 args: tuple or list of strings";
1422
1423static PyObject *
1424posix_spawnv(self, args)
1425 PyObject *self;
1426 PyObject *args;
1427{
1428 char *path;
1429 PyObject *argv;
1430 char **argvlist;
1431 int mode, i, argc;
1432 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1433
1434 /* spawnv has three arguments: (mode, path, argv), where
1435 argv is a list or tuple of strings. */
1436
1437 if (!PyArg_Parse(args, "(isO)", &mode, &path, &argv))
1438 return NULL;
1439 if (PyList_Check(argv)) {
1440 argc = PyList_Size(argv);
1441 getitem = PyList_GetItem;
1442 }
1443 else if (PyTuple_Check(argv)) {
1444 argc = PyTuple_Size(argv);
1445 getitem = PyTuple_GetItem;
1446 }
1447 else {
1448 badarg:
1449 PyErr_BadArgument();
1450 return NULL;
1451 }
1452
1453 argvlist = PyMem_NEW(char *, argc+1);
1454 if (argvlist == NULL)
1455 return NULL;
1456 for (i = 0; i < argc; i++) {
1457 if (!PyArg_Parse((*getitem)(argv, i), "s", &argvlist[i])) {
1458 PyMem_DEL(argvlist);
1459 goto badarg;
1460 }
1461 }
1462 argvlist[argc] = NULL;
1463
Guido van Rossum246bc171999-02-01 23:54:31 +00001464 if (mode == _OLD_P_OVERLAY)
1465 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001466 i = _spawnv(mode, path, argvlist);
1467
1468 PyMem_DEL(argvlist);
1469
1470 if (i == -1)
1471 return posix_error();
1472 else
1473 return Py_BuildValue("i", i);
1474}
1475
1476
1477static char posix_spawnve__doc__[] =
Fred Drakea6dff3e1999-02-01 22:24:40 +00001478"spawnve(mode, path, args, env)\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001479Execute a path with arguments and environment, replacing current process.\n\
1480\n\
Fred Drakea6dff3e1999-02-01 22:24:40 +00001481 mode: mode of process creation\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00001482 path: path of executable file\n\
1483 args: tuple or list of arguments\n\
1484 env: dictonary of strings mapping to strings";
1485
1486static PyObject *
1487posix_spawnve(self, args)
1488 PyObject *self;
1489 PyObject *args;
1490{
1491 char *path;
1492 PyObject *argv, *env;
1493 char **argvlist;
1494 char **envlist;
1495 PyObject *key, *val, *keys=NULL, *vals=NULL, *res=NULL;
1496 int mode, i, pos, argc, envc;
1497 PyObject *(*getitem) Py_PROTO((PyObject *, int));
1498
1499 /* spawnve has four arguments: (mode, path, argv, env), where
1500 argv is a list or tuple of strings and env is a dictionary
1501 like posix.environ. */
1502
1503 if (!PyArg_Parse(args, "(isOO)", &mode, &path, &argv, &env))
1504 return NULL;
1505 if (PyList_Check(argv)) {
1506 argc = PyList_Size(argv);
1507 getitem = PyList_GetItem;
1508 }
1509 else if (PyTuple_Check(argv)) {
1510 argc = PyTuple_Size(argv);
1511 getitem = PyTuple_GetItem;
1512 }
1513 else {
1514 PyErr_SetString(PyExc_TypeError, "argv must be tuple or list");
1515 return NULL;
1516 }
1517 if (!PyMapping_Check(env)) {
1518 PyErr_SetString(PyExc_TypeError, "env must be mapping object");
1519 return NULL;
1520 }
1521
1522 argvlist = PyMem_NEW(char *, argc+1);
1523 if (argvlist == NULL) {
1524 PyErr_NoMemory();
1525 return NULL;
1526 }
1527 for (i = 0; i < argc; i++) {
1528 if (!PyArg_Parse((*getitem)(argv, i),
1529 "s;argv must be list of strings",
1530 &argvlist[i]))
1531 {
1532 goto fail_1;
1533 }
1534 }
1535 argvlist[argc] = NULL;
1536
1537 i = PyMapping_Length(env);
1538 envlist = PyMem_NEW(char *, i + 1);
1539 if (envlist == NULL) {
1540 PyErr_NoMemory();
1541 goto fail_1;
1542 }
1543 envc = 0;
1544 keys = PyMapping_Keys(env);
1545 vals = PyMapping_Values(env);
1546 if (!keys || !vals)
1547 goto fail_2;
1548
1549 for (pos = 0; pos < i; pos++) {
1550 char *p, *k, *v;
1551
1552 key = PyList_GetItem(keys, pos);
1553 val = PyList_GetItem(vals, pos);
1554 if (!key || !val)
1555 goto fail_2;
1556
1557 if (!PyArg_Parse(key, "s;non-string key in env", &k) ||
1558 !PyArg_Parse(val, "s;non-string value in env", &v))
1559 {
1560 goto fail_2;
1561 }
1562 p = PyMem_NEW(char, PyString_Size(key)+PyString_Size(val) + 2);
1563 if (p == NULL) {
1564 PyErr_NoMemory();
1565 goto fail_2;
1566 }
1567 sprintf(p, "%s=%s", k, v);
1568 envlist[envc++] = p;
1569 }
1570 envlist[envc] = 0;
1571
Guido van Rossum246bc171999-02-01 23:54:31 +00001572 if (mode == _OLD_P_OVERLAY)
1573 mode = _P_OVERLAY;
Guido van Rossuma1065681999-01-25 23:20:23 +00001574 i = _spawnve(mode, path, argvlist, envlist);
1575 if (i == -1)
1576 (void) posix_error();
1577 else
1578 res = Py_BuildValue("i", i);
1579
1580 fail_2:
1581 while (--envc >= 0)
1582 PyMem_DEL(envlist[envc]);
1583 PyMem_DEL(envlist);
1584 fail_1:
1585 PyMem_DEL(argvlist);
1586 Py_XDECREF(vals);
1587 Py_XDECREF(keys);
1588 return res;
1589}
1590#endif /* HAVE_SPAWNV */
1591
1592
Guido van Rossumad0ee831995-03-01 10:34:45 +00001593#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001594static char posix_fork__doc__[] =
1595"fork() -> pid\n\
1596Fork a child process.\n\
1597\n\
1598Return 0 to child process and PID of child to parent process.";
1599
Barry Warsaw53699e91996-12-10 23:23:01 +00001600static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001601posix_fork(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001602 PyObject *self;
1603 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001604{
1605 int pid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001606 if (!PyArg_NoArgs(args))
Guido van Rossum50e61dc1992-03-27 17:22:31 +00001607 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001608 pid = fork();
1609 if (pid == -1)
1610 return posix_error();
Guido van Rossum359bcaa1997-11-14 22:24:28 +00001611 PyOS_AfterFork();
Barry Warsaw53699e91996-12-10 23:23:01 +00001612 return PyInt_FromLong((long)pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00001613}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001614#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001615
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001616
Guido van Rossumad0ee831995-03-01 10:34:45 +00001617#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001618static char posix_getegid__doc__[] =
1619"getegid() -> egid\n\
1620Return the current process's effective group id.";
1621
Barry Warsaw53699e91996-12-10 23:23:01 +00001622static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001623posix_getegid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001624 PyObject *self;
1625 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001626{
Barry Warsaw53699e91996-12-10 23:23:01 +00001627 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001628 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001629 return PyInt_FromLong((long)getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001630}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001631#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001633
Guido van Rossumad0ee831995-03-01 10:34:45 +00001634#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001635static char posix_geteuid__doc__[] =
1636"geteuid() -> euid\n\
1637Return the current process's effective user id.";
1638
Barry Warsaw53699e91996-12-10 23:23:01 +00001639static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001640posix_geteuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001641 PyObject *self;
1642 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001643{
Barry Warsaw53699e91996-12-10 23:23:01 +00001644 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001645 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001646 return PyInt_FromLong((long)geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001647}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001648#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001649
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001650
Guido van Rossumad0ee831995-03-01 10:34:45 +00001651#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001652static char posix_getgid__doc__[] =
1653"getgid() -> gid\n\
1654Return the current process's group id.";
1655
Barry Warsaw53699e91996-12-10 23:23:01 +00001656static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001657posix_getgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001658 PyObject *self;
1659 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001660{
Barry Warsaw53699e91996-12-10 23:23:01 +00001661 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001662 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001663 return PyInt_FromLong((long)getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001664}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001665#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001666
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001667
1668static char posix_getpid__doc__[] =
1669"getpid() -> pid\n\
1670Return the current process id";
1671
Barry Warsaw53699e91996-12-10 23:23:01 +00001672static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001673posix_getpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001674 PyObject *self;
1675 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001676{
Barry Warsaw53699e91996-12-10 23:23:01 +00001677 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001678 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001679 return PyInt_FromLong((long)getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001680}
1681
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001682
Guido van Rossumb6775db1994-08-01 11:34:53 +00001683#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001684static char posix_getpgrp__doc__[] =
1685"getpgrp() -> pgrp\n\
1686Return the current process group id.";
1687
Barry Warsaw53699e91996-12-10 23:23:01 +00001688static PyObject *
Guido van Rossum04814471991-06-04 20:23:49 +00001689posix_getpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001690 PyObject *self;
1691 PyObject *args;
Guido van Rossum04814471991-06-04 20:23:49 +00001692{
Barry Warsaw53699e91996-12-10 23:23:01 +00001693 if (!PyArg_NoArgs(args))
Guido van Rossum04814471991-06-04 20:23:49 +00001694 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00001695#ifdef GETPGRP_HAVE_ARG
Barry Warsaw53699e91996-12-10 23:23:01 +00001696 return PyInt_FromLong((long)getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001697#else /* GETPGRP_HAVE_ARG */
Barry Warsaw53699e91996-12-10 23:23:01 +00001698 return PyInt_FromLong((long)getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001699#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00001700}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001701#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00001702
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001703
Guido van Rossumb6775db1994-08-01 11:34:53 +00001704#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001705static char posix_setpgrp__doc__[] =
1706"setpgrp() -> None\n\
1707Make this process a session leader.";
1708
Barry Warsaw53699e91996-12-10 23:23:01 +00001709static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00001710posix_setpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001711 PyObject *self;
1712 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001713{
Barry Warsaw53699e91996-12-10 23:23:01 +00001714 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00001715 return NULL;
Guido van Rossum64933891994-10-20 21:56:42 +00001716#ifdef SETPGRP_HAVE_ARG
Guido van Rossumc2670a01992-09-13 20:07:29 +00001717 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001718#else /* SETPGRP_HAVE_ARG */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001719 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00001720#endif /* SETPGRP_HAVE_ARG */
Guido van Rossum687dd131993-05-17 08:34:16 +00001721 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001722 Py_INCREF(Py_None);
1723 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00001724}
1725
Guido van Rossumb6775db1994-08-01 11:34:53 +00001726#endif /* HAVE_SETPGRP */
1727
Guido van Rossumad0ee831995-03-01 10:34:45 +00001728#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001729static char posix_getppid__doc__[] =
1730"getppid() -> ppid\n\
1731Return the parent's process id.";
1732
Barry Warsaw53699e91996-12-10 23:23:01 +00001733static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001734posix_getppid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001735 PyObject *self;
1736 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001737{
Barry Warsaw53699e91996-12-10 23:23:01 +00001738 if (!PyArg_NoArgs(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001739 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001740 return PyInt_FromLong((long)getppid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00001741}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001742#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001743
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001744
Guido van Rossumad0ee831995-03-01 10:34:45 +00001745#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001746static char posix_getuid__doc__[] =
1747"getuid() -> uid\n\
1748Return the current process's user id.";
1749
Barry Warsaw53699e91996-12-10 23:23:01 +00001750static PyObject *
Guido van Rossum46003ff1992-05-15 11:05:24 +00001751posix_getuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001752 PyObject *self;
1753 PyObject *args;
Guido van Rossum46003ff1992-05-15 11:05:24 +00001754{
Barry Warsaw53699e91996-12-10 23:23:01 +00001755 if (!PyArg_NoArgs(args))
Guido van Rossum46003ff1992-05-15 11:05:24 +00001756 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001757 return PyInt_FromLong((long)getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00001758}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001759#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00001760
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001761
Guido van Rossumad0ee831995-03-01 10:34:45 +00001762#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001763static char posix_kill__doc__[] =
1764"kill(pid, sig) -> None\n\
1765Kill a process with a signal.";
1766
Barry Warsaw53699e91996-12-10 23:23:01 +00001767static PyObject *
Guido van Rossum85e3b011991-06-03 12:42:10 +00001768posix_kill(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001769 PyObject *self;
1770 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001771{
1772 int pid, sig;
Barry Warsaw53699e91996-12-10 23:23:01 +00001773 if (!PyArg_Parse(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +00001774 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001775#if defined(PYOS_OS2)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001776 if (sig == XCPT_SIGNAL_INTR || sig == XCPT_SIGNAL_BREAK) {
1777 APIRET rc;
1778 if ((rc = DosSendSignalException(pid, sig)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001779 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001780
1781 } else if (sig == XCPT_SIGNAL_KILLPROC) {
1782 APIRET rc;
1783 if ((rc = DosKillProcess(DKP_PROCESS, pid)) != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001784 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001785
1786 } else
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001787 return NULL; /* Unrecognized Signal Requested */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001788#else
Guido van Rossum85e3b011991-06-03 12:42:10 +00001789 if (kill(pid, sig) == -1)
1790 return posix_error();
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001791#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00001792 Py_INCREF(Py_None);
1793 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00001794}
Guido van Rossumad0ee831995-03-01 10:34:45 +00001795#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00001796
Guido van Rossumc0125471996-06-28 18:55:32 +00001797#ifdef HAVE_PLOCK
1798
1799#ifdef HAVE_SYS_LOCK_H
1800#include <sys/lock.h>
1801#endif
1802
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001803static char posix_plock__doc__[] =
1804"plock(op) -> None\n\
1805Lock program segments into memory.";
1806
Barry Warsaw53699e91996-12-10 23:23:01 +00001807static PyObject *
Guido van Rossumc0125471996-06-28 18:55:32 +00001808posix_plock(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001809 PyObject *self;
1810 PyObject *args;
Guido van Rossumc0125471996-06-28 18:55:32 +00001811{
1812 int op;
Barry Warsaw53699e91996-12-10 23:23:01 +00001813 if (!PyArg_Parse(args, "i", &op))
Guido van Rossumc0125471996-06-28 18:55:32 +00001814 return NULL;
1815 if (plock(op) == -1)
1816 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001817 Py_INCREF(Py_None);
1818 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00001819}
1820#endif
1821
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001822
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001823#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001824static char posix_popen__doc__[] =
1825"popen(command [, mode='r' [, bufsize]]) -> pipe\n\
1826Open a pipe to/from a command returning a file object.";
1827
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001828#if defined(PYOS_OS2)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001829static int
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001830async_system(const char *command)
1831{
1832 char *p, errormsg[256], args[1024];
1833 RESULTCODES rcodes;
1834 APIRET rc;
1835 char *shell = getenv("COMSPEC");
1836 if (!shell)
1837 shell = "cmd";
1838
1839 strcpy(args, shell);
1840 p = &args[ strlen(args)+1 ];
1841 strcpy(p, "/c ");
1842 strcat(p, command);
1843 p += strlen(p) + 1;
1844 *p = '\0';
1845
1846 rc = DosExecPgm(errormsg, sizeof(errormsg),
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001847 EXEC_ASYNC, /* Execute Async w/o Wait for Results */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001848 args,
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001849 NULL, /* Inherit Parent's Environment */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001850 &rcodes, shell);
1851 return rc;
1852}
1853
Guido van Rossumd48f2521997-12-05 22:19:34 +00001854static FILE *
1855popen(const char *command, const char *mode, int pipesize, int *err)
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001856{
1857 HFILE rhan, whan;
1858 FILE *retfd = NULL;
1859 APIRET rc = DosCreatePipe(&rhan, &whan, pipesize);
1860
Guido van Rossumd48f2521997-12-05 22:19:34 +00001861 if (rc != NO_ERROR) {
1862 *err = rc;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001863 return NULL; /* ERROR - Unable to Create Anon Pipe */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001864 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001865
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001866 if (strchr(mode, 'r') != NULL) { /* Treat Command as a Data Source */
1867 int oldfd = dup(1); /* Save STDOUT Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001868
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001869 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1870 close(1); /* Make STDOUT Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001871
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001872 if (dup2(whan, 1) == 0) { /* Connect STDOUT to Pipe Write Side */
1873 DosClose(whan); /* Close Now-Unused Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001874
1875 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001876 retfd = fdopen(rhan, mode); /* And Return Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001877 }
1878
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001879 dup2(oldfd, 1); /* Reconnect STDOUT to Original Handle */
1880 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001881
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001882 close(oldfd); /* And Close Saved STDOUT Handle */
1883 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001884
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001885 } else if (strchr(mode, 'w')) { /* Treat Command as a Data Sink */
1886 int oldfd = dup(0); /* Save STDIN Handle in Another Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001887
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001888 DosEnterCritSec(); /* Stop Other Threads While Changing Handles */
1889 close(0); /* Make STDIN Available for Reallocation */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001890
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001891 if (dup2(rhan, 0) == 0) { /* Connect STDIN to Pipe Read Side */
1892 DosClose(rhan); /* Close Now-Unused Pipe Read Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001893
1894 if (async_system(command) == NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001895 retfd = fdopen(whan, mode); /* And Return Pipe Write Handle */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001896 }
1897
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001898 dup2(oldfd, 0); /* Reconnect STDIN to Original Handle */
1899 DosExitCritSec(); /* Now Allow Other Threads to Run */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001900
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001901 close(oldfd); /* And Close Saved STDIN Handle */
1902 return retfd; /* Return fd of Pipe or NULL if Error */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001903
Guido van Rossumd48f2521997-12-05 22:19:34 +00001904 } else {
1905 *err = ERROR_INVALID_ACCESS;
Guido van Rossumc5a0f531997-12-02 20:36:02 +00001906 return NULL; /* ERROR - Invalid Mode (Neither Read nor Write) */
Guido van Rossumd48f2521997-12-05 22:19:34 +00001907 }
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001908}
1909
1910static PyObject *
1911posix_popen(self, args)
1912 PyObject *self;
1913 PyObject *args;
1914{
1915 char *name;
1916 char *mode = "r";
Guido van Rossumd48f2521997-12-05 22:19:34 +00001917 int err, bufsize = -1;
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001918 FILE *fp;
1919 PyObject *f;
1920 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
1921 return NULL;
1922 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd48f2521997-12-05 22:19:34 +00001923 fp = popen(name, mode, (bufsize > 0) ? bufsize : 4096, &err);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001924 Py_END_ALLOW_THREADS
1925 if (fp == NULL)
Guido van Rossumd48f2521997-12-05 22:19:34 +00001926 return os2_error(err);
1927
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001928 f = PyFile_FromFile(fp, name, mode, fclose);
1929 if (f != NULL)
1930 PyFile_SetBufSize(f, bufsize);
1931 return f;
1932}
1933
1934#else
Barry Warsaw53699e91996-12-10 23:23:01 +00001935static PyObject *
Guido van Rossum3b066191991-06-04 19:40:25 +00001936posix_popen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001937 PyObject *self;
1938 PyObject *args;
Guido van Rossum3b066191991-06-04 19:40:25 +00001939{
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001940 char *name;
1941 char *mode = "r";
1942 int bufsize = -1;
Guido van Rossum3b066191991-06-04 19:40:25 +00001943 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00001944 PyObject *f;
1945 if (!PyArg_ParseTuple(args, "s|si", &name, &mode, &bufsize))
Guido van Rossum3b066191991-06-04 19:40:25 +00001946 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00001947 Py_BEGIN_ALLOW_THREADS
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001948 fp = popen(name, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00001949 Py_END_ALLOW_THREADS
Guido van Rossum3b066191991-06-04 19:40:25 +00001950 if (fp == NULL)
1951 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001952 f = PyFile_FromFile(fp, name, mode, pclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001953 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00001954 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00001955 return f;
Guido van Rossum3b066191991-06-04 19:40:25 +00001956}
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00001957#endif
1958
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001959#endif /* HAVE_POPEN */
Guido van Rossum3b066191991-06-04 19:40:25 +00001960
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001961
Guido van Rossumb6775db1994-08-01 11:34:53 +00001962#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001963static char posix_setuid__doc__[] =
1964"setuid(uid) -> None\n\
1965Set the current process's user id.";
Barry Warsaw53699e91996-12-10 23:23:01 +00001966static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001967posix_setuid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001968 PyObject *self;
1969 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001970{
1971 int uid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001972 if (!PyArg_Parse(args, "i", &uid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001973 return NULL;
1974 if (setuid(uid) < 0)
1975 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001976 Py_INCREF(Py_None);
1977 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001978}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00001979#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001980
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001981
Guido van Rossumb6775db1994-08-01 11:34:53 +00001982#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00001983static char posix_setgid__doc__[] =
1984"setgid(gid) -> None\n\
1985Set the current process's group id.";
1986
Barry Warsaw53699e91996-12-10 23:23:01 +00001987static PyObject *
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001988posix_setgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00001989 PyObject *self;
1990 PyObject *args;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001991{
1992 int gid;
Barry Warsaw53699e91996-12-10 23:23:01 +00001993 if (!PyArg_Parse(args, "i", &gid))
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001994 return NULL;
1995 if (setgid(gid) < 0)
1996 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00001997 Py_INCREF(Py_None);
1998 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00001999}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002000#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00002001
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002002
Guido van Rossumb6775db1994-08-01 11:34:53 +00002003#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002004static char posix_waitpid__doc__[] =
2005"waitpid(pid, options) -> (pid, status)\n\
2006Wait for completion of a give child process.";
2007
Barry Warsaw53699e91996-12-10 23:23:01 +00002008static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002009posix_waitpid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002010 PyObject *self;
2011 PyObject *args;
Guido van Rossum85e3b011991-06-03 12:42:10 +00002012{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002013 int pid, options;
2014#ifdef UNION_WAIT
2015 union wait status;
2016#define status_i (status.w_status)
2017#else
2018 int status;
2019#define status_i status
2020#endif
2021 status_i = 0;
2022
Barry Warsaw53699e91996-12-10 23:23:01 +00002023 if (!PyArg_Parse(args, "(ii)", &pid, &options))
Guido van Rossum21803b81992-08-09 12:55:27 +00002024 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002025 Py_BEGIN_ALLOW_THREADS
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002026#ifdef NeXT
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002027 pid = wait4(pid, &status, options, NULL);
Guido van Rossume6a3aa61999-02-01 16:15:30 +00002028#else
2029 pid = waitpid(pid, &status, options);
2030#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002031 Py_END_ALLOW_THREADS
Guido van Rossum85e3b011991-06-03 12:42:10 +00002032 if (pid == -1)
2033 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +00002034 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002035 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum21803b81992-08-09 12:55:27 +00002036}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002037#endif /* HAVE_WAITPID */
Guido van Rossum21803b81992-08-09 12:55:27 +00002038
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002039
Guido van Rossumad0ee831995-03-01 10:34:45 +00002040#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002041static char posix_wait__doc__[] =
2042"wait() -> (pid, status)\n\
2043Wait for completion of a child process.";
2044
Barry Warsaw53699e91996-12-10 23:23:01 +00002045static PyObject *
Guido van Rossum21803b81992-08-09 12:55:27 +00002046posix_wait(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002047 PyObject *self;
2048 PyObject *args;
Guido van Rossum21803b81992-08-09 12:55:27 +00002049{
2050 int pid, sts;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002051#ifdef UNION_WAIT
2052 union wait status;
2053#define status_i (status.w_status)
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002054#else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002055 int status;
2056#define status_i status
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002057#endif
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002058 status_i = 0;
2059 Py_BEGIN_ALLOW_THREADS
2060 pid = wait(&status);
Barry Warsaw53699e91996-12-10 23:23:01 +00002061 Py_END_ALLOW_THREADS
Guido van Rossum21803b81992-08-09 12:55:27 +00002062 if (pid == -1)
2063 return posix_error();
2064 else
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002065 return Py_BuildValue("ii", pid, status_i);
Guido van Rossum85e3b011991-06-03 12:42:10 +00002066}
Guido van Rossumad0ee831995-03-01 10:34:45 +00002067#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00002068
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002069
2070static char posix_lstat__doc__[] =
2071"lstat(path) -> (mode,ino,dev,nlink,uid,gid,size,atime,mtime,ctime)\n\
2072Like stat(path), but do not follow symbolic links.";
2073
Barry Warsaw53699e91996-12-10 23:23:01 +00002074static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002075posix_lstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002076 PyObject *self;
2077 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002078{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002079#ifdef HAVE_LSTAT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002080 return posix_do_stat(self, args, lstat);
Guido van Rossumb6775db1994-08-01 11:34:53 +00002081#else /* !HAVE_LSTAT */
2082 return posix_do_stat(self, args, stat);
2083#endif /* !HAVE_LSTAT */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002084}
2085
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002086
Guido van Rossumb6775db1994-08-01 11:34:53 +00002087#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002088static char posix_readlink__doc__[] =
2089"readlink(path) -> path\n\
2090Return a string representing the path to which the symbolic link points.";
2091
Barry Warsaw53699e91996-12-10 23:23:01 +00002092static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002093posix_readlink(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002094 PyObject *self;
2095 PyObject *args;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002096{
Guido van Rossumb6775db1994-08-01 11:34:53 +00002097 char buf[MAXPATHLEN];
Guido van Rossumef0a00e1992-01-27 16:51:30 +00002098 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002099 int n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002100 if (!PyArg_Parse(args, "s", &path))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002101 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002102 Py_BEGIN_ALLOW_THREADS
Guido van Rossum50e61dc1992-03-27 17:22:31 +00002103 n = readlink(path, buf, (int) sizeof buf);
Barry Warsaw53699e91996-12-10 23:23:01 +00002104 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002105 if (n < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002106 return posix_error_with_filename(path);
Barry Warsaw53699e91996-12-10 23:23:01 +00002107 return PyString_FromStringAndSize(buf, n);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002108}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002109#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002110
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002111
Guido van Rossumb6775db1994-08-01 11:34:53 +00002112#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002113static char posix_symlink__doc__[] =
2114"symlink(src, dst) -> None\n\
2115Create a symbolic link.";
2116
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002117static PyObject *
2118posix_symlink(self, args)
2119 PyObject *self;
2120 PyObject *args;
2121{
2122 return posix_2str(args, symlink);
2123}
2124#endif /* HAVE_SYMLINK */
2125
2126
2127#ifdef HAVE_TIMES
2128#ifndef HZ
2129#define HZ 60 /* Universal constant :-) */
2130#endif /* HZ */
2131
Guido van Rossumd48f2521997-12-05 22:19:34 +00002132#if defined(PYCC_VACPP) && defined(PYOS_OS2)
2133static long
2134system_uptime()
2135{
2136 ULONG value = 0;
2137
2138 Py_BEGIN_ALLOW_THREADS
2139 DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &value, sizeof(value));
2140 Py_END_ALLOW_THREADS
2141
2142 return value;
2143}
2144
2145static PyObject *
2146posix_times(self, args)
2147 PyObject *self;
2148 PyObject *args;
2149{
2150 if (!PyArg_NoArgs(args))
2151 return NULL;
2152
2153 /* Currently Only Uptime is Provided -- Others Later */
2154 return Py_BuildValue("ddddd",
2155 (double)0 /* t.tms_utime / HZ */,
2156 (double)0 /* t.tms_stime / HZ */,
2157 (double)0 /* t.tms_cutime / HZ */,
2158 (double)0 /* t.tms_cstime / HZ */,
2159 (double)system_uptime() / 1000);
2160}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002161#else /* not OS2 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002162static PyObject *
Guido van Rossum22db57e1992-04-05 14:25:30 +00002163posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002164 PyObject *self;
2165 PyObject *args;
Guido van Rossum22db57e1992-04-05 14:25:30 +00002166{
2167 struct tms t;
2168 clock_t c;
Barry Warsaw53699e91996-12-10 23:23:01 +00002169 if (!PyArg_NoArgs(args))
Guido van Rossum22db57e1992-04-05 14:25:30 +00002170 return NULL;
2171 errno = 0;
2172 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +00002173 if (c == (clock_t) -1)
2174 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002175 return Py_BuildValue("ddddd",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002176 (double)t.tms_utime / HZ,
2177 (double)t.tms_stime / HZ,
2178 (double)t.tms_cutime / HZ,
2179 (double)t.tms_cstime / HZ,
2180 (double)c / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +00002181}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002182#endif /* not OS2 */
Guido van Rossumb6775db1994-08-01 11:34:53 +00002183#endif /* HAVE_TIMES */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002184
2185
Guido van Rossum87755a21996-09-07 00:59:43 +00002186#ifdef MS_WIN32
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002187#define HAVE_TIMES /* so the method table will pick it up */
Barry Warsaw53699e91996-12-10 23:23:01 +00002188static PyObject *
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002189posix_times(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002190 PyObject *self;
2191 PyObject *args;
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002192{
2193 FILETIME create, exit, kernel, user;
2194 HANDLE hProc;
Barry Warsaw53699e91996-12-10 23:23:01 +00002195 if (!PyArg_NoArgs(args))
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002196 return NULL;
2197 hProc = GetCurrentProcess();
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002198 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
2199 /* The fields of a FILETIME structure are the hi and lo part
2200 of a 64-bit value expressed in 100 nanosecond units.
2201 1e7 is one second in such units; 1e-7 the inverse.
2202 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
2203 */
Barry Warsaw53699e91996-12-10 23:23:01 +00002204 return Py_BuildValue(
2205 "ddddd",
Guido van Rossumb8c3cbd1999-02-16 14:37:28 +00002206 (double)(kernel.dwHighDateTime*429.4967296 +
2207 kernel.dwLowDateTime*1e-7),
2208 (double)(user.dwHighDateTime*429.4967296 +
2209 user.dwLowDateTime*1e-7),
Barry Warsaw53699e91996-12-10 23:23:01 +00002210 (double)0,
2211 (double)0,
2212 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00002213}
Guido van Rossum8d665e61996-06-26 18:22:49 +00002214#endif /* MS_WIN32 */
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002215
2216#ifdef HAVE_TIMES
Roger E. Masse0318fd61997-06-05 22:07:58 +00002217static char posix_times__doc__[] =
2218"times() -> (utime, stime, cutime, cstime, elapsed_time)\n\
2219Return a tuple of floating point numbers indicating process times.";
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00002220#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002221
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002222
Guido van Rossumb6775db1994-08-01 11:34:53 +00002223#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002224static char posix_setsid__doc__[] =
2225"setsid() -> None\n\
2226Call the system call setsid().";
2227
Barry Warsaw53699e91996-12-10 23:23:01 +00002228static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002229posix_setsid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002230 PyObject *self;
2231 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002232{
Barry Warsaw53699e91996-12-10 23:23:01 +00002233 if (!PyArg_NoArgs(args))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002234 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002235 if (setsid() < 0)
2236 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002237 Py_INCREF(Py_None);
2238 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002239}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002240#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002241
Guido van Rossumb6775db1994-08-01 11:34:53 +00002242#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002243static char posix_setpgid__doc__[] =
2244"setpgid(pid, pgrp) -> None\n\
2245Call the system call setpgid().";
2246
Barry Warsaw53699e91996-12-10 23:23:01 +00002247static PyObject *
Guido van Rossumc2670a01992-09-13 20:07:29 +00002248posix_setpgid(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002249 PyObject *self;
2250 PyObject *args;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002251{
2252 int pid, pgrp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002253 if (!PyArg_Parse(args, "(ii)", &pid, &pgrp))
Guido van Rossumc2670a01992-09-13 20:07:29 +00002254 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002255 if (setpgid(pid, pgrp) < 0)
2256 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002257 Py_INCREF(Py_None);
2258 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00002259}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002260#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00002261
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002262
Guido van Rossumb6775db1994-08-01 11:34:53 +00002263#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002264static char posix_tcgetpgrp__doc__[] =
2265"tcgetpgrp(fd) -> pgid\n\
2266Return the process group associated with the terminal given by a fd.";
2267
Barry Warsaw53699e91996-12-10 23:23:01 +00002268static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002269posix_tcgetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002270 PyObject *self;
2271 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002272{
2273 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002274 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002275 return NULL;
2276 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002277 if (pgid < 0)
2278 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002279 return PyInt_FromLong((long)pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00002280}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002281#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00002282
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002283
Guido van Rossumb6775db1994-08-01 11:34:53 +00002284#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002285static char posix_tcsetpgrp__doc__[] =
2286"tcsetpgrp(fd, pgid) -> None\n\
2287Set the process group associated with the terminal given by a fd.";
2288
Barry Warsaw53699e91996-12-10 23:23:01 +00002289static PyObject *
Guido van Rossum7066dd71992-09-17 17:54:56 +00002290posix_tcsetpgrp(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002291 PyObject *self;
2292 PyObject *args;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002293{
2294 int fd, pgid;
Barry Warsaw53699e91996-12-10 23:23:01 +00002295 if (!PyArg_Parse(args, "(ii)", &fd, &pgid))
Guido van Rossum7066dd71992-09-17 17:54:56 +00002296 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00002297 if (tcsetpgrp(fd, pgid) < 0)
2298 return posix_error();
Barry Warsaw43d68b81996-12-19 22:10:44 +00002299 Py_INCREF(Py_None);
Barry Warsaw53699e91996-12-10 23:23:01 +00002300 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00002301}
Guido van Rossumb6775db1994-08-01 11:34:53 +00002302#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00002303
Guido van Rossum687dd131993-05-17 08:34:16 +00002304/* Functions acting on file descriptors */
2305
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002306static char posix_open__doc__[] =
2307"open(filename, flag [, mode=0777]) -> fd\n\
2308Open a file (for low level IO).";
2309
Barry Warsaw53699e91996-12-10 23:23:01 +00002310static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002311posix_open(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002312 PyObject *self;
2313 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002314{
2315 char *file;
2316 int flag;
2317 int mode = 0777;
2318 int fd;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002319 if (!PyArg_ParseTuple(args, "si|i", &file, &flag, &mode))
2320 return NULL;
2321
Barry Warsaw53699e91996-12-10 23:23:01 +00002322 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002323 fd = open(file, flag, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002324 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002325 if (fd < 0)
Barry Warsawd58d7641998-07-23 16:14:40 +00002326 return posix_error_with_filename(file);
Barry Warsaw53699e91996-12-10 23:23:01 +00002327 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002328}
2329
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002330
2331static char posix_close__doc__[] =
2332"close(fd) -> None\n\
2333Close a file descriptor (for low level IO).";
2334
Barry Warsaw53699e91996-12-10 23:23:01 +00002335static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002336posix_close(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002337 PyObject *self;
2338 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002339{
2340 int fd, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002341 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002342 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002343 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002344 res = close(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002345 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002346 if (res < 0)
2347 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002348 Py_INCREF(Py_None);
2349 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002350}
2351
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002352
2353static char posix_dup__doc__[] =
2354"dup(fd) -> fd2\n\
2355Return a duplicate of a file descriptor.";
2356
Barry Warsaw53699e91996-12-10 23:23:01 +00002357static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002358posix_dup(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002359 PyObject *self;
2360 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002361{
2362 int fd;
Barry Warsaw53699e91996-12-10 23:23:01 +00002363 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002364 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002365 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002366 fd = dup(fd);
Barry Warsaw53699e91996-12-10 23:23:01 +00002367 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002368 if (fd < 0)
2369 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002370 return PyInt_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00002371}
2372
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002373
2374static char posix_dup2__doc__[] =
2375"dup2(fd, fd2) -> None\n\
2376Duplicate file descriptor.";
2377
Barry Warsaw53699e91996-12-10 23:23:01 +00002378static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002379posix_dup2(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002380 PyObject *self;
2381 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002382{
2383 int fd, fd2, res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002384 if (!PyArg_Parse(args, "(ii)", &fd, &fd2))
Guido van Rossum687dd131993-05-17 08:34:16 +00002385 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002386 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002387 res = dup2(fd, fd2);
Barry Warsaw53699e91996-12-10 23:23:01 +00002388 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002389 if (res < 0)
2390 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002391 Py_INCREF(Py_None);
2392 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00002393}
2394
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002395
2396static char posix_lseek__doc__[] =
2397"lseek(fd, pos, how) -> newpos\n\
2398Set the current position of a file descriptor.";
2399
Barry Warsaw53699e91996-12-10 23:23:01 +00002400static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002401posix_lseek(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002402 PyObject *self;
2403 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002404{
2405 int fd, how;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002406 off_t pos, res;
2407 PyObject *posobj;
2408 if (!PyArg_Parse(args, "(iOi)", &fd, &posobj, &how))
Guido van Rossum687dd131993-05-17 08:34:16 +00002409 return NULL;
2410#ifdef SEEK_SET
2411 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
2412 switch (how) {
2413 case 0: how = SEEK_SET; break;
2414 case 1: how = SEEK_CUR; break;
2415 case 2: how = SEEK_END; break;
2416 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00002417#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00002418
2419#if !defined(HAVE_LARGEFILE_SUPPORT)
2420 pos = PyInt_AsLong(posobj);
2421#else
2422 pos = PyLong_Check(posobj) ?
2423 PyLong_AsLongLong(posobj) : PyInt_AsLong(posobj);
2424#endif
2425 if (PyErr_Occurred())
2426 return NULL;
2427
Barry Warsaw53699e91996-12-10 23:23:01 +00002428 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002429 res = lseek(fd, pos, how);
Barry Warsaw53699e91996-12-10 23:23:01 +00002430 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002431 if (res < 0)
2432 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002433
2434#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002435 return PyInt_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002436#else
2437 return PyLong_FromLongLong(res);
2438#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002439}
2440
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002441
2442static char posix_read__doc__[] =
2443"read(fd, buffersize) -> string\n\
2444Read a file descriptor.";
2445
Barry Warsaw53699e91996-12-10 23:23:01 +00002446static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002447posix_read(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002448 PyObject *self;
2449 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002450{
Guido van Rossum8bac5461996-06-11 18:38:48 +00002451 int fd, size, n;
Barry Warsaw53699e91996-12-10 23:23:01 +00002452 PyObject *buffer;
2453 if (!PyArg_Parse(args, "(ii)", &fd, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002454 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002455 buffer = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002456 if (buffer == NULL)
2457 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002458 Py_BEGIN_ALLOW_THREADS
2459 n = read(fd, PyString_AsString(buffer), size);
2460 Py_END_ALLOW_THREADS
Guido van Rossum8bac5461996-06-11 18:38:48 +00002461 if (n < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002462 Py_DECREF(buffer);
Guido van Rossum687dd131993-05-17 08:34:16 +00002463 return posix_error();
2464 }
Guido van Rossum8bac5461996-06-11 18:38:48 +00002465 if (n != size)
Barry Warsaw53699e91996-12-10 23:23:01 +00002466 _PyString_Resize(&buffer, n);
Guido van Rossum687dd131993-05-17 08:34:16 +00002467 return buffer;
2468}
2469
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002470
2471static char posix_write__doc__[] =
2472"write(fd, string) -> byteswritten\n\
2473Write a string to a file descriptor.";
2474
Barry Warsaw53699e91996-12-10 23:23:01 +00002475static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002476posix_write(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002477 PyObject *self;
2478 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002479{
2480 int fd, size;
2481 char *buffer;
Barry Warsaw53699e91996-12-10 23:23:01 +00002482 if (!PyArg_Parse(args, "(is#)", &fd, &buffer, &size))
Guido van Rossum687dd131993-05-17 08:34:16 +00002483 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002484 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002485 size = write(fd, buffer, size);
Barry Warsaw53699e91996-12-10 23:23:01 +00002486 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002487 if (size < 0)
2488 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002489 return PyInt_FromLong((long)size);
Guido van Rossum687dd131993-05-17 08:34:16 +00002490}
2491
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002492
2493static char posix_fstat__doc__[]=
2494"fstat(fd) -> (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
2495Like stat(), but for an open file descriptor.";
2496
Barry Warsaw53699e91996-12-10 23:23:01 +00002497static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002498posix_fstat(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002499 PyObject *self;
2500 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002501{
2502 int fd;
2503 struct stat st;
2504 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002505 if (!PyArg_Parse(args, "i", &fd))
Guido van Rossum687dd131993-05-17 08:34:16 +00002506 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002507 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002508 res = fstat(fd, &st);
Barry Warsaw53699e91996-12-10 23:23:01 +00002509 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002510 if (res != 0)
2511 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00002512#if !defined(HAVE_LARGEFILE_SUPPORT)
Barry Warsaw53699e91996-12-10 23:23:01 +00002513 return Py_BuildValue("(llllllllll)",
Barry Warsaw43d68b81996-12-19 22:10:44 +00002514 (long)st.st_mode,
2515 (long)st.st_ino,
2516 (long)st.st_dev,
2517 (long)st.st_nlink,
2518 (long)st.st_uid,
2519 (long)st.st_gid,
2520 (long)st.st_size,
2521 (long)st.st_atime,
2522 (long)st.st_mtime,
2523 (long)st.st_ctime);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002524#else
2525 return Py_BuildValue("(lLllllLlll)",
2526 (long)st.st_mode,
2527 (LONG_LONG)st.st_ino,
2528 (long)st.st_dev,
2529 (long)st.st_nlink,
2530 (long)st.st_uid,
2531 (long)st.st_gid,
2532 (LONG_LONG)st.st_size,
2533 (long)st.st_atime,
2534 (long)st.st_mtime,
2535 (long)st.st_ctime);
2536#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002537}
2538
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002539
2540static char posix_fdopen__doc__[] =
2541"fdopen(fd, [, mode='r' [, bufsize]]) -> file_object\n\
2542Return an open file object connected to a file descriptor.";
2543
Barry Warsaw53699e91996-12-10 23:23:01 +00002544static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002545posix_fdopen(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002546 PyObject *self;
2547 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002548{
Barry Warsaw53699e91996-12-10 23:23:01 +00002549 extern int fclose Py_PROTO((FILE *));
Guido van Rossum687dd131993-05-17 08:34:16 +00002550 int fd;
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002551 char *mode = "r";
2552 int bufsize = -1;
Guido van Rossum687dd131993-05-17 08:34:16 +00002553 FILE *fp;
Barry Warsaw53699e91996-12-10 23:23:01 +00002554 PyObject *f;
2555 if (!PyArg_ParseTuple(args, "i|si", &fd, &mode, &bufsize))
Guido van Rossum687dd131993-05-17 08:34:16 +00002556 return NULL;
Barry Warsaw43d68b81996-12-19 22:10:44 +00002557
Barry Warsaw53699e91996-12-10 23:23:01 +00002558 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002559 fp = fdopen(fd, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002560 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002561 if (fp == NULL)
2562 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002563 f = PyFile_FromFile(fp, "(fdopen)", mode, fclose);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002564 if (f != NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002565 PyFile_SetBufSize(f, bufsize);
Guido van Rossuma6a1e531995-01-10 15:36:38 +00002566 return f;
Guido van Rossum687dd131993-05-17 08:34:16 +00002567}
2568
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002569
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002570#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002571static char posix_pipe__doc__[] =
2572"pipe() -> (read_end, write_end)\n\
2573Create a pipe.";
2574
Barry Warsaw53699e91996-12-10 23:23:01 +00002575static PyObject *
Guido van Rossum687dd131993-05-17 08:34:16 +00002576posix_pipe(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002577 PyObject *self;
2578 PyObject *args;
Guido van Rossum687dd131993-05-17 08:34:16 +00002579{
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002580#if defined(PYOS_OS2)
2581 HFILE read, write;
2582 APIRET rc;
2583
2584 if (!PyArg_Parse(args, ""))
2585 return NULL;
2586
2587 Py_BEGIN_ALLOW_THREADS
2588 rc = DosCreatePipe( &read, &write, 4096);
2589 Py_END_ALLOW_THREADS
2590 if (rc != NO_ERROR)
Guido van Rossumd48f2521997-12-05 22:19:34 +00002591 return os2_error(rc);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002592
2593 return Py_BuildValue("(ii)", read, write);
2594#else
Guido van Rossum8d665e61996-06-26 18:22:49 +00002595#if !defined(MS_WIN32)
Guido van Rossum687dd131993-05-17 08:34:16 +00002596 int fds[2];
2597 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002598 if (!PyArg_Parse(args, ""))
Guido van Rossum687dd131993-05-17 08:34:16 +00002599 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002600 Py_BEGIN_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002601 res = pipe(fds);
Barry Warsaw53699e91996-12-10 23:23:01 +00002602 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00002603 if (res != 0)
2604 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002605 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002606#else /* MS_WIN32 */
Guido van Rossum794d8131994-08-23 13:48:48 +00002607 HANDLE read, write;
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002608 int read_fd, write_fd;
Guido van Rossum794d8131994-08-23 13:48:48 +00002609 BOOL ok;
Barry Warsaw53699e91996-12-10 23:23:01 +00002610 if (!PyArg_Parse(args, ""))
Guido van Rossum794d8131994-08-23 13:48:48 +00002611 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002612 Py_BEGIN_ALLOW_THREADS
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002613 ok = CreatePipe(&read, &write, NULL, 0);
Barry Warsaw53699e91996-12-10 23:23:01 +00002614 Py_END_ALLOW_THREADS
Guido van Rossum794d8131994-08-23 13:48:48 +00002615 if (!ok)
2616 return posix_error();
Guido van Rossumb3f9f4b1998-06-12 15:05:15 +00002617 read_fd = _open_osfhandle((long)read, 0);
2618 write_fd = _open_osfhandle((long)write, 1);
2619 return Py_BuildValue("(ii)", read_fd, write_fd);
Guido van Rossum8d665e61996-06-26 18:22:49 +00002620#endif /* MS_WIN32 */
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00002621#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00002622}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002623#endif /* HAVE_PIPE */
2624
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002625
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002626#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002627static char posix_mkfifo__doc__[] =
2628"mkfifo(file, [, mode=0666]) -> None\n\
2629Create a FIFO (a POSIX named pipe).";
2630
Barry Warsaw53699e91996-12-10 23:23:01 +00002631static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002632posix_mkfifo(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002633 PyObject *self;
2634 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002635{
2636 char *file;
2637 int mode = 0666;
2638 int res;
Barry Warsaw53699e91996-12-10 23:23:01 +00002639 if (!PyArg_ParseTuple(args, "s|i", &file, &mode))
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002640 return NULL;
Barry Warsaw53699e91996-12-10 23:23:01 +00002641 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002642 res = mkfifo(file, mode);
Barry Warsaw53699e91996-12-10 23:23:01 +00002643 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002644 if (res < 0)
2645 return posix_error();
Barry Warsaw53699e91996-12-10 23:23:01 +00002646 Py_INCREF(Py_None);
2647 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002648}
2649#endif
2650
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002651
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002652#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002653static char posix_ftruncate__doc__[] =
2654"ftruncate(fd, length) -> None\n\
2655Truncate a file to a specified length.";
2656
Barry Warsaw53699e91996-12-10 23:23:01 +00002657static PyObject *
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002658posix_ftruncate(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002659 PyObject *self; /* Not used */
2660 PyObject *args;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002661{
2662 int fd;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002663 off_t length;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002664 int res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002665 PyObject *lenobj;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002666
Guido van Rossum94f6f721999-01-06 18:42:14 +00002667 if (!PyArg_Parse(args, "(iO)", &fd, &lenobj))
2668 return NULL;
2669
2670#if !defined(HAVE_LARGEFILE_SUPPORT)
2671 length = PyInt_AsLong(lenobj);
2672#else
2673 length = PyLong_Check(lenobj) ?
2674 PyLong_AsLongLong(lenobj) : PyInt_AsLong(lenobj);
2675#endif
2676 if (PyErr_Occurred())
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002677 return NULL;
2678
Barry Warsaw53699e91996-12-10 23:23:01 +00002679 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002680 res = ftruncate(fd, length);
Barry Warsaw53699e91996-12-10 23:23:01 +00002681 Py_END_ALLOW_THREADS
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002682 if (res < 0) {
Barry Warsaw53699e91996-12-10 23:23:01 +00002683 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002684 return NULL;
2685 }
Barry Warsaw53699e91996-12-10 23:23:01 +00002686 Py_INCREF(Py_None);
2687 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00002688}
2689#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00002690
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002691#ifdef NeXT
2692#define HAVE_PUTENV
2693/* Steve Spicklemire got this putenv from NeXTAnswers */
2694static int
2695putenv(char *newval)
2696{
2697 extern char **environ;
2698
2699 static int firstTime = 1;
2700 char **ep;
2701 char *cp;
2702 int esiz;
2703 char *np;
2704
2705 if (!(np = strchr(newval, '=')))
2706 return 1;
2707 *np = '\0';
2708
2709 /* look it up */
2710 for (ep=environ ; *ep ; ep++)
2711 {
2712 /* this should always be true... */
2713 if (cp = strchr(*ep, '='))
2714 {
2715 *cp = '\0';
2716 if (!strcmp(*ep, newval))
2717 {
2718 /* got it! */
2719 *cp = '=';
2720 break;
2721 }
2722 *cp = '=';
2723 }
2724 else
2725 {
2726 *np = '=';
2727 return 1;
2728 }
2729 }
2730
2731 *np = '=';
2732 if (*ep)
2733 {
2734 /* the string was already there:
2735 just replace it with the new one */
2736 *ep = newval;
2737 return 0;
2738 }
2739
2740 /* expand environ by one */
2741 for (esiz=2, ep=environ ; *ep ; ep++)
2742 esiz++;
2743 if (firstTime)
2744 {
2745 char **epp;
2746 char **newenv;
2747 if (!(newenv = malloc(esiz * sizeof(char *))))
2748 return 1;
2749
2750 for (ep=environ, epp=newenv ; *ep ;)
2751 *epp++ = *ep++;
2752 *epp++ = newval;
2753 *epp = (char *) 0;
2754 environ = newenv;
2755 }
2756 else
2757 {
2758 if (!(environ = realloc(environ, esiz * sizeof(char *))))
2759 return 1;
2760 environ[esiz - 2] = newval;
2761 environ[esiz - 1] = (char *) 0;
2762 firstTime = 0;
2763 }
2764
2765 return 0;
2766}
Guido van Rossumc6ef2041997-08-21 02:30:45 +00002767#endif /* NeXT */
Guido van Rossumb9f866c1997-05-22 15:12:39 +00002768
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002769
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002770#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002771static char posix_putenv__doc__[] =
2772"putenv(key, value) -> None\n\
2773Change or add an environment variable.";
2774
Guido van Rossumbcc20741998-08-04 22:53:56 +00002775#ifdef __BEOS__
2776/* We have putenv(), but not in the headers (as of PR2). - [cjh] */
2777int putenv( const char *str );
2778#endif
2779
Barry Warsaw53699e91996-12-10 23:23:01 +00002780static PyObject *
Guido van Rossumb6a47161997-09-15 22:54:34 +00002781posix_putenv(self, args)
Barry Warsaw53699e91996-12-10 23:23:01 +00002782 PyObject *self;
2783 PyObject *args;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002784{
2785 char *s1, *s2;
2786 char *new;
2787
Barry Warsaw53699e91996-12-10 23:23:01 +00002788 if (!PyArg_ParseTuple(args, "ss", &s1, &s2))
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002789 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00002790
2791#if defined(PYOS_OS2)
2792 if (stricmp(s1, "BEGINLIBPATH") == 0) {
2793 APIRET rc;
2794
2795 if (strlen(s2) == 0) /* If New Value is an Empty String */
2796 s2 = NULL; /* Then OS/2 API Wants a NULL to Undefine It */
2797
2798 rc = DosSetExtLIBPATH(s2, BEGIN_LIBPATH);
2799 if (rc != NO_ERROR)
2800 return os2_error(rc);
2801
2802 } else if (stricmp(s1, "ENDLIBPATH") == 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, END_LIBPATH);
2809 if (rc != NO_ERROR)
2810 return os2_error(rc);
2811 } else {
2812#endif
2813
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002814 /* XXX This leaks memory -- not easy to fix :-( */
2815 if ((new = malloc(strlen(s1) + strlen(s2) + 2)) == NULL)
Barry Warsaw53699e91996-12-10 23:23:01 +00002816 return PyErr_NoMemory();
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002817 (void) sprintf(new, "%s=%s", s1, s2);
2818 if (putenv(new)) {
2819 posix_error();
2820 return NULL;
2821 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00002822
2823#if defined(PYOS_OS2)
2824 }
2825#endif
Barry Warsaw53699e91996-12-10 23:23:01 +00002826 Py_INCREF(Py_None);
2827 return Py_None;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002828}
Guido van Rossumb6a47161997-09-15 22:54:34 +00002829#endif /* putenv */
2830
2831#ifdef HAVE_STRERROR
2832static char posix_strerror__doc__[] =
2833"strerror(code) -> string\n\
2834Translate an error code to a message string.";
2835
2836PyObject *
2837posix_strerror(self, args)
2838 PyObject *self;
2839 PyObject *args;
2840{
2841 int code;
2842 char *message;
2843 if (!PyArg_ParseTuple(args, "i", &code))
2844 return NULL;
2845 message = strerror(code);
2846 if (message == NULL) {
2847 PyErr_SetString(PyExc_ValueError,
2848 "strerror code out of range");
2849 return NULL;
2850 }
2851 return PyString_FromString(message);
2852}
2853#endif /* strerror */
2854
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00002855
Guido van Rossumc9641791998-08-04 15:26:23 +00002856#ifdef HAVE_SYS_WAIT_H
2857
2858#ifdef WIFSTOPPED
2859static char posix_WIFSTOPPED__doc__[] =
2860"WIFSTOPPED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002861Return true if the process returning 'status' was stopped.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002862
2863static PyObject *
2864posix_WIFSTOPPED(self, args)
2865 PyObject *self;
2866 PyObject *args;
2867{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002868#ifdef UNION_WAIT
2869 union wait status;
2870#define status_i (status.w_status)
2871#else
2872 int status;
2873#define status_i status
2874#endif
2875 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002876
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002877 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002878 {
2879 return NULL;
2880 }
2881
2882 return Py_BuildValue("i", WIFSTOPPED(status));
2883}
2884#endif /* WIFSTOPPED */
2885
2886#ifdef WIFSIGNALED
2887static char posix_WIFSIGNALED__doc__[] =
2888"WIFSIGNALED(status) -> Boolean\n\
Guido van Rossum3366d1c1999-02-23 18:34:43 +00002889Return true if the process returning 'status' was terminated by a signal.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002890
2891static PyObject *
2892posix_WIFSIGNALED(self, args)
2893 PyObject *self;
2894 PyObject *args;
2895{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002896#ifdef UNION_WAIT
2897 union wait status;
2898#define status_i (status.w_status)
2899#else
2900 int status;
2901#define status_i status
2902#endif
2903 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002904
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002905 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002906 {
2907 return NULL;
2908 }
2909
2910 return Py_BuildValue("i", WIFSIGNALED(status));
2911}
2912#endif /* WIFSIGNALED */
2913
2914#ifdef WIFEXITED
2915static char posix_WIFEXITED__doc__[] =
2916"WIFEXITED(status) -> Boolean\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002917Return true if the process returning 'status' exited using the exit()\n\
2918system call.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002919
2920static PyObject *
2921posix_WIFEXITED(self, args)
2922 PyObject *self;
2923 PyObject *args;
2924{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002925#ifdef UNION_WAIT
2926 union wait status;
2927#define status_i (status.w_status)
2928#else
2929 int status;
2930#define status_i status
2931#endif
2932 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002933
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002934 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002935 {
2936 return NULL;
2937 }
2938
2939 return Py_BuildValue("i", WIFEXITED(status));
2940}
2941#endif /* WIFEXITED */
2942
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002943#ifdef WEXITSTATUS
Guido van Rossumc9641791998-08-04 15:26:23 +00002944static char posix_WEXITSTATUS__doc__[] =
2945"WEXITSTATUS(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002946Return the process return code from 'status'.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002947
2948static PyObject *
2949posix_WEXITSTATUS(self, args)
2950 PyObject *self;
2951 PyObject *args;
2952{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002953#ifdef UNION_WAIT
2954 union wait status;
2955#define status_i (status.w_status)
2956#else
2957 int status;
2958#define status_i status
2959#endif
2960 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002961
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002962 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002963 {
2964 return NULL;
2965 }
2966
2967 return Py_BuildValue("i", WEXITSTATUS(status));
2968}
2969#endif /* WEXITSTATUS */
2970
2971#ifdef WTERMSIG
2972static char posix_WTERMSIG__doc__[] =
2973"WTERMSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00002974Return the signal that terminated the process that provided the 'status'\n\
2975value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00002976
2977static PyObject *
2978posix_WTERMSIG(self, args)
2979 PyObject *self;
2980 PyObject *args;
2981{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002982#ifdef UNION_WAIT
2983 union wait status;
2984#define status_i (status.w_status)
2985#else
2986 int status;
2987#define status_i status
2988#endif
2989 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00002990
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00002991 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00002992 {
2993 return NULL;
2994 }
2995
2996 return Py_BuildValue("i", WTERMSIG(status));
2997}
2998#endif /* WTERMSIG */
2999
3000#ifdef WSTOPSIG
3001static char posix_WSTOPSIG__doc__[] =
3002"WSTOPSIG(status) -> integer\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00003003Return the signal that stopped the process that provided the 'status' value.";
Guido van Rossumc9641791998-08-04 15:26:23 +00003004
3005static PyObject *
3006posix_WSTOPSIG(self, args)
3007 PyObject *self;
3008 PyObject *args;
3009{
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003010#ifdef UNION_WAIT
3011 union wait status;
3012#define status_i (status.w_status)
3013#else
3014 int status;
3015#define status_i status
3016#endif
3017 status_i = 0;
Guido van Rossumc9641791998-08-04 15:26:23 +00003018
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00003019 if (!PyArg_Parse(args, "i", &status_i))
Guido van Rossumc9641791998-08-04 15:26:23 +00003020 {
3021 return NULL;
3022 }
3023
3024 return Py_BuildValue("i", WSTOPSIG(status));
3025}
3026#endif /* WSTOPSIG */
3027
3028#endif /* HAVE_SYS_WAIT_H */
3029
3030
Guido van Rossum94f6f721999-01-06 18:42:14 +00003031#if defined(HAVE_FSTATVFS)
3032#include <sys/statvfs.h>
3033
3034static char posix_fstatvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003035"fstatvfs(fd) -> \n\
3036 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003037Perform an fstatvfs system call on the given fd.";
3038
3039static PyObject *
3040posix_fstatvfs(self, args)
3041 PyObject *self;
3042 PyObject *args;
3043{
3044 int fd, res;
3045 struct statvfs st;
3046 if (!PyArg_ParseTuple(args, "i", &fd))
3047 return NULL;
3048 Py_BEGIN_ALLOW_THREADS
3049 res = fstatvfs(fd, &st);
3050 Py_END_ALLOW_THREADS
3051 if (res != 0)
3052 return posix_error();
3053#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003054 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003055 (long) st.f_bsize,
3056 (long) st.f_frsize,
3057 (long) st.f_blocks,
3058 (long) st.f_bfree,
3059 (long) st.f_bavail,
3060 (long) st.f_files,
3061 (long) st.f_ffree,
3062 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003063 (long) st.f_flag,
3064 (long) st.f_namemax);
3065#else
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003066 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003067 (long) st.f_bsize,
3068 (long) st.f_frsize,
3069 (LONG_LONG) st.f_blocks,
3070 (LONG_LONG) st.f_bfree,
3071 (LONG_LONG) st.f_bavail,
3072 (LONG_LONG) st.f_files,
3073 (LONG_LONG) st.f_ffree,
3074 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003075 (long) st.f_flag,
3076 (long) st.f_namemax);
3077#endif
3078}
3079#endif /* HAVE_FSTATVFS */
3080
3081
3082#if defined(HAVE_STATVFS)
3083#include <sys/statvfs.h>
3084
3085static char posix_statvfs__doc__[] =
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003086"statvfs(path) -> \n\
3087 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax)\n\
Guido van Rossum94f6f721999-01-06 18:42:14 +00003088Perform a statvfs system call on the given path.";
3089
3090static PyObject *
3091posix_statvfs(self, args)
3092 PyObject *self;
3093 PyObject *args;
3094{
3095 char *path;
3096 int res;
3097 struct statvfs st;
3098 if (!PyArg_ParseTuple(args, "s", &path))
3099 return NULL;
3100 Py_BEGIN_ALLOW_THREADS
3101 res = statvfs(path, &st);
3102 Py_END_ALLOW_THREADS
3103 if (res != 0)
3104 return posix_error_with_filename(path);
3105#if !defined(HAVE_LARGEFILE_SUPPORT)
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003106 return Py_BuildValue("(llllllllll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003107 (long) st.f_bsize,
3108 (long) st.f_frsize,
3109 (long) st.f_blocks,
3110 (long) st.f_bfree,
3111 (long) st.f_bavail,
3112 (long) st.f_files,
3113 (long) st.f_ffree,
3114 (long) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003115 (long) st.f_flag,
3116 (long) st.f_namemax);
3117#else /* HAVE_LARGEFILE_SUPPORT */
Guido van Rossum0c9608c1999-02-03 16:32:37 +00003118 return Py_BuildValue("(llLLLLLLll)",
Guido van Rossum94f6f721999-01-06 18:42:14 +00003119 (long) st.f_bsize,
3120 (long) st.f_frsize,
3121 (LONG_LONG) st.f_blocks,
3122 (LONG_LONG) st.f_bfree,
3123 (LONG_LONG) st.f_bavail,
3124 (LONG_LONG) st.f_files,
3125 (LONG_LONG) st.f_ffree,
3126 (LONG_LONG) st.f_favail,
Guido van Rossum94f6f721999-01-06 18:42:14 +00003127 (long) st.f_flag,
3128 (long) st.f_namemax);
3129#endif
3130}
3131#endif /* HAVE_STATVFS */
3132
3133
Barry Warsaw53699e91996-12-10 23:23:01 +00003134static PyMethodDef posix_methods[] = {
Guido van Rossum94f6f721999-01-06 18:42:14 +00003135 {"access", posix_access, 0, posix_access__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003136#ifdef HAVE_TTYNAME
Guido van Rossum94f6f721999-01-06 18:42:14 +00003137 {"ttyname", posix_ttyname, 0, posix_ttyname__doc__},
Guido van Rossumd371ff11999-01-25 16:12:23 +00003138#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003139 {"chdir", posix_chdir, 0, posix_chdir__doc__},
3140 {"chmod", posix_chmod, 0, posix_chmod__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003141#ifdef HAVE_CHOWN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003142 {"chown", posix_chown, 0, posix_chown__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003143#endif /* HAVE_CHOWN */
Guido van Rossum36bc6801995-06-14 22:54:23 +00003144#ifdef HAVE_GETCWD
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003145 {"getcwd", posix_getcwd, 0, posix_getcwd__doc__},
Guido van Rossum36bc6801995-06-14 22:54:23 +00003146#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00003147#ifdef HAVE_LINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003148 {"link", posix_link, 0, posix_link__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003149#endif /* HAVE_LINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003150 {"listdir", posix_listdir, 0, posix_listdir__doc__},
3151 {"lstat", posix_lstat, 0, posix_lstat__doc__},
3152 {"mkdir", posix_mkdir, 1, posix_mkdir__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003153#ifdef HAVE_NICE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003154 {"nice", posix_nice, 0, posix_nice__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003155#endif /* HAVE_NICE */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003156#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003157 {"readlink", posix_readlink, 0, posix_readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003158#endif /* HAVE_READLINK */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003159 {"rename", posix_rename, 0, posix_rename__doc__},
3160 {"rmdir", posix_rmdir, 0, posix_rmdir__doc__},
3161 {"stat", posix_stat, 0, posix_stat__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003162#ifdef HAVE_SYMLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003163 {"symlink", posix_symlink, 0, posix_symlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003164#endif /* HAVE_SYMLINK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003165#ifdef HAVE_SYSTEM
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003166 {"system", posix_system, 0, posix_system__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003167#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003168 {"umask", posix_umask, 0, posix_umask__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003169#ifdef HAVE_UNAME
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003170 {"uname", posix_uname, 0, posix_uname__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003171#endif /* HAVE_UNAME */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003172 {"unlink", posix_unlink, 0, posix_unlink__doc__},
3173 {"remove", posix_unlink, 0, posix_remove__doc__},
3174 {"utime", posix_utime, 0, posix_utime__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003175#ifdef HAVE_TIMES
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003176 {"times", posix_times, 0, posix_times__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003177#endif /* HAVE_TIMES */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003178 {"_exit", posix__exit, 0, posix__exit__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003179#ifdef HAVE_EXECV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003180 {"execv", posix_execv, 0, posix_execv__doc__},
3181 {"execve", posix_execve, 0, posix_execve__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003182#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +00003183#ifdef HAVE_SPAWNV
3184 {"spawnv", posix_spawnv, 0, posix_spawnv__doc__},
3185 {"spawnve", posix_spawnve, 0, posix_spawnve__doc__},
3186#endif /* HAVE_SPAWNV */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003187#ifdef HAVE_FORK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003188 {"fork", posix_fork, 0, posix_fork__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003189#endif /* HAVE_FORK */
3190#ifdef HAVE_GETEGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003191 {"getegid", posix_getegid, 0, posix_getegid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003192#endif /* HAVE_GETEGID */
3193#ifdef HAVE_GETEUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003194 {"geteuid", posix_geteuid, 0, posix_geteuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003195#endif /* HAVE_GETEUID */
3196#ifdef HAVE_GETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003197 {"getgid", posix_getgid, 0, posix_getgid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003198#endif /* HAVE_GETGID */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003199 {"getpid", posix_getpid, 0, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +00003200#ifdef HAVE_GETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003201 {"getpgrp", posix_getpgrp, 0, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003202#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003203#ifdef HAVE_GETPPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003204 {"getppid", posix_getppid, 0, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003205#endif /* HAVE_GETPPID */
3206#ifdef HAVE_GETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003207 {"getuid", posix_getuid, 0, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003208#endif /* HAVE_GETUID */
3209#ifdef HAVE_KILL
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003210 {"kill", posix_kill, 0, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003211#endif /* HAVE_KILL */
Guido van Rossumc0125471996-06-28 18:55:32 +00003212#ifdef HAVE_PLOCK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003213 {"plock", posix_plock, 0, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +00003214#endif /* HAVE_PLOCK */
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003215#ifdef HAVE_POPEN
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003216 {"popen", posix_popen, 1, posix_popen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003217#endif /* HAVE_POPEN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003218#ifdef HAVE_SETUID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003219 {"setuid", posix_setuid, 0, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003220#endif /* HAVE_SETUID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003221#ifdef HAVE_SETGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003222 {"setgid", posix_setgid, 0, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003223#endif /* HAVE_SETGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003224#ifdef HAVE_SETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003225 {"setpgrp", posix_setpgrp, 0, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003226#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +00003227#ifdef HAVE_WAIT
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003228 {"wait", posix_wait, 0, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +00003229#endif /* HAVE_WAIT */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003230#ifdef HAVE_WAITPID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003231 {"waitpid", posix_waitpid, 0, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003232#endif /* HAVE_WAITPID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003233#ifdef HAVE_SETSID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003234 {"setsid", posix_setsid, 0, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003235#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003236#ifdef HAVE_SETPGID
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003237 {"setpgid", posix_setpgid, 0, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003238#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003239#ifdef HAVE_TCGETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003240 {"tcgetpgrp", posix_tcgetpgrp, 0, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003241#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003242#ifdef HAVE_TCSETPGRP
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003243 {"tcsetpgrp", posix_tcsetpgrp, 0, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003244#endif /* HAVE_TCSETPGRP */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003245 {"open", posix_open, 1, posix_open__doc__},
3246 {"close", posix_close, 0, posix_close__doc__},
3247 {"dup", posix_dup, 0, posix_dup__doc__},
3248 {"dup2", posix_dup2, 0, posix_dup2__doc__},
3249 {"lseek", posix_lseek, 0, posix_lseek__doc__},
3250 {"read", posix_read, 0, posix_read__doc__},
3251 {"write", posix_write, 0, posix_write__doc__},
3252 {"fstat", posix_fstat, 0, posix_fstat__doc__},
3253 {"fdopen", posix_fdopen, 1, posix_fdopen__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003254#ifdef HAVE_PIPE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003255 {"pipe", posix_pipe, 0, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003256#endif
3257#ifdef HAVE_MKFIFO
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003258 {"mkfifo", posix_mkfifo, 1, posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003259#endif
3260#ifdef HAVE_FTRUNCATE
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003261 {"ftruncate", posix_ftruncate, 1, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003262#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003263#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003264 {"putenv", posix_putenv, 1, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00003265#endif
Guido van Rossumb6a47161997-09-15 22:54:34 +00003266#ifdef HAVE_STRERROR
3267 {"strerror", posix_strerror, 1, posix_strerror__doc__},
3268#endif
Guido van Rossum21142a01999-01-08 21:05:37 +00003269#ifdef HAVE_FSYNC
3270 {"fsync", posix_fsync, 0, posix_fsync__doc__},
3271#endif
3272#ifdef HAVE_FDATASYNC
3273 {"fdatasync", posix_fdatasync, 0, posix_fdatasync__doc__},
3274#endif
Guido van Rossumc9641791998-08-04 15:26:23 +00003275#ifdef HAVE_SYS_WAIT_H
3276#ifdef WIFSTOPPED
3277 {"WIFSTOPPED", posix_WIFSTOPPED, 0, posix_WIFSTOPPED__doc__},
3278#endif /* WIFSTOPPED */
3279#ifdef WIFSIGNALED
3280 {"WIFSIGNALED", posix_WIFSIGNALED, 0, posix_WIFSIGNALED__doc__},
3281#endif /* WIFSIGNALED */
3282#ifdef WIFEXITED
3283 {"WIFEXITED", posix_WIFEXITED, 0, posix_WIFEXITED__doc__},
3284#endif /* WIFEXITED */
3285#ifdef WEXITSTATUS
3286 {"WEXITSTATUS", posix_WEXITSTATUS, 0, posix_WEXITSTATUS__doc__},
3287#endif /* WEXITSTATUS */
3288#ifdef WTERMSIG
3289 {"WTERMSIG", posix_WTERMSIG, 0, posix_WTERMSIG__doc__},
3290#endif /* WTERMSIG */
3291#ifdef WSTOPSIG
3292 {"WSTOPSIG", posix_WSTOPSIG, 0, posix_WSTOPSIG__doc__},
3293#endif /* WSTOPSIG */
3294#endif /* HAVE_SYS_WAIT_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00003295#ifdef HAVE_FSTATVFS
3296 {"fstatvfs", posix_fstatvfs, 1, posix_fstatvfs__doc__},
3297#endif
3298#ifdef HAVE_STATVFS
3299 {"statvfs", posix_statvfs, 1, posix_statvfs__doc__},
3300#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003301 {NULL, NULL} /* Sentinel */
3302};
3303
3304
Barry Warsaw4a342091996-12-19 23:50:02 +00003305static int
3306ins(d, symbol, value)
3307 PyObject* d;
3308 char* symbol;
3309 long value;
3310{
3311 PyObject* v = PyInt_FromLong(value);
3312 if (!v || PyDict_SetItemString(d, symbol, v) < 0)
3313 return -1; /* triggers fatal error */
3314
3315 Py_DECREF(v);
3316 return 0;
3317}
3318
Guido van Rossumd48f2521997-12-05 22:19:34 +00003319#if defined(PYOS_OS2)
3320/* Insert Platform-Specific Constant Values (Strings & Numbers) of Common Use */
3321static int insertvalues(PyObject *d)
3322{
3323 APIRET rc;
3324 ULONG values[QSV_MAX+1];
3325 PyObject *v;
3326 char *ver, tmp[10];
3327
3328 Py_BEGIN_ALLOW_THREADS
3329 rc = DosQuerySysInfo(1, QSV_MAX, &values[1], sizeof(values));
3330 Py_END_ALLOW_THREADS
3331
3332 if (rc != NO_ERROR) {
3333 os2_error(rc);
3334 return -1;
3335 }
3336
3337 if (ins(d, "meminstalled", values[QSV_TOTPHYSMEM])) return -1;
3338 if (ins(d, "memkernel", values[QSV_TOTRESMEM])) return -1;
3339 if (ins(d, "memvirtual", values[QSV_TOTAVAILMEM])) return -1;
3340 if (ins(d, "maxpathlen", values[QSV_MAX_PATH_LENGTH])) return -1;
3341 if (ins(d, "maxnamelen", values[QSV_MAX_COMP_LENGTH])) return -1;
3342 if (ins(d, "revision", values[QSV_VERSION_REVISION])) return -1;
3343 if (ins(d, "timeslice", values[QSV_MIN_SLICE])) return -1;
3344
3345 switch (values[QSV_VERSION_MINOR]) {
3346 case 0: ver = "2.00"; break;
3347 case 10: ver = "2.10"; break;
3348 case 11: ver = "2.11"; break;
3349 case 30: ver = "3.00"; break;
3350 case 40: ver = "4.00"; break;
3351 case 50: ver = "5.00"; break;
3352 default:
3353 sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
3354 values[QSV_VERSION_MINOR]);
3355 ver = &tmp[0];
3356 }
3357
3358 /* Add Indicator of the Version of the Operating System */
3359 v = PyString_FromString(ver);
3360 if (!v || PyDict_SetItemString(d, "version", v) < 0)
3361 return -1;
3362 Py_DECREF(v);
3363
3364 /* Add Indicator of Which Drive was Used to Boot the System */
3365 tmp[0] = 'A' + values[QSV_BOOT_DRIVE] - 1;
3366 tmp[1] = ':';
3367 tmp[2] = '\0';
3368
3369 v = PyString_FromString(tmp);
3370 if (!v || PyDict_SetItemString(d, "bootdrive", v) < 0)
3371 return -1;
3372 Py_DECREF(v);
3373
3374 return 0;
3375}
3376#endif
3377
Barry Warsaw4a342091996-12-19 23:50:02 +00003378static int
3379all_ins(d)
3380 PyObject* d;
3381{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003382#ifdef F_OK
3383 if (ins(d, "F_OK", (long)F_OK)) return -1;
3384#endif
3385#ifdef R_OK
3386 if (ins(d, "R_OK", (long)R_OK)) return -1;
3387#endif
3388#ifdef W_OK
3389 if (ins(d, "W_OK", (long)W_OK)) return -1;
3390#endif
3391#ifdef X_OK
3392 if (ins(d, "X_OK", (long)X_OK)) return -1;
3393#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003394#ifdef WNOHANG
3395 if (ins(d, "WNOHANG", (long)WNOHANG)) return -1;
3396#endif
3397#ifdef O_RDONLY
3398 if (ins(d, "O_RDONLY", (long)O_RDONLY)) return -1;
3399#endif
3400#ifdef O_WRONLY
3401 if (ins(d, "O_WRONLY", (long)O_WRONLY)) return -1;
3402#endif
3403#ifdef O_RDWR
3404 if (ins(d, "O_RDWR", (long)O_RDWR)) return -1;
3405#endif
3406#ifdef O_NDELAY
3407 if (ins(d, "O_NDELAY", (long)O_NDELAY)) return -1;
3408#endif
3409#ifdef O_NONBLOCK
3410 if (ins(d, "O_NONBLOCK", (long)O_NONBLOCK)) return -1;
3411#endif
3412#ifdef O_APPEND
3413 if (ins(d, "O_APPEND", (long)O_APPEND)) return -1;
3414#endif
3415#ifdef O_DSYNC
3416 if (ins(d, "O_DSYNC", (long)O_DSYNC)) return -1;
3417#endif
3418#ifdef O_RSYNC
3419 if (ins(d, "O_RSYNC", (long)O_RSYNC)) return -1;
3420#endif
3421#ifdef O_SYNC
3422 if (ins(d, "O_SYNC", (long)O_SYNC)) return -1;
3423#endif
3424#ifdef O_NOCTTY
3425 if (ins(d, "O_NOCTTY", (long)O_NOCTTY)) return -1;
3426#endif
3427#ifdef O_CREAT
3428 if (ins(d, "O_CREAT", (long)O_CREAT)) return -1;
3429#endif
3430#ifdef O_EXCL
3431 if (ins(d, "O_EXCL", (long)O_EXCL)) return -1;
3432#endif
3433#ifdef O_TRUNC
3434 if (ins(d, "O_TRUNC", (long)O_TRUNC)) return -1;
3435#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +00003436#ifdef O_BINARY
3437 if (ins(d, "O_BINARY", (long)O_BINARY)) return -1;
3438#endif
3439#ifdef O_TEXT
3440 if (ins(d, "O_TEXT", (long)O_TEXT)) return -1;
3441#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00003442
Guido van Rossum246bc171999-02-01 23:54:31 +00003443#ifdef HAVE_SPAWNV
Guido van Rossum7d385291999-02-16 19:38:04 +00003444 if (ins(d, "P_WAIT", (long)_P_WAIT)) return -1;
3445 if (ins(d, "P_NOWAIT", (long)_P_NOWAIT)) return -1;
3446 if (ins(d, "P_OVERLAY", (long)_OLD_P_OVERLAY)) return -1;
3447 if (ins(d, "P_NOWAITO", (long)_P_NOWAITO)) return -1;
3448 if (ins(d, "P_DETACH", (long)_P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +00003449#endif
3450
Guido van Rossumd48f2521997-12-05 22:19:34 +00003451#if defined(PYOS_OS2)
3452 if (insertvalues(d)) return -1;
3453#endif
Barry Warsaw4a342091996-12-19 23:50:02 +00003454 return 0;
3455}
3456
3457
Guido van Rossumc5a0f531997-12-02 20:36:02 +00003458#if ( defined(_MSC_VER) || defined(__WATCOMC__) ) && !defined(__QNX__)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003459#define INITFUNC initnt
3460#define MODNAME "nt"
3461#else
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003462#if defined(PYOS_OS2)
3463#define INITFUNC initos2
3464#define MODNAME "os2"
3465#else
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003466#define INITFUNC initposix
3467#define MODNAME "posix"
3468#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +00003469#endif
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003470
Guido van Rossum3886bb61998-12-04 18:50:17 +00003471DL_EXPORT(void)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003472INITFUNC()
Guido van Rossumb6775db1994-08-01 11:34:53 +00003473{
Barry Warsaw53699e91996-12-10 23:23:01 +00003474 PyObject *m, *d, *v;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003475
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003476 m = Py_InitModule4(MODNAME,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003477 posix_methods,
3478 posix__doc__,
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003479 (PyObject *)NULL,
3480 PYTHON_API_VERSION);
Barry Warsaw53699e91996-12-10 23:23:01 +00003481 d = PyModule_GetDict(m);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003482
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003483 /* Initialize environ dictionary */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003484 v = convertenviron();
Barry Warsaw53699e91996-12-10 23:23:01 +00003485 if (v == NULL || PyDict_SetItemString(d, "environ", v) != 0)
Guido van Rossum0cb96de1997-10-01 04:29:29 +00003486 return;
Barry Warsaw53699e91996-12-10 23:23:01 +00003487 Py_DECREF(v);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003488
Barry Warsaw4a342091996-12-19 23:50:02 +00003489 if (all_ins(d))
Barry Warsaw4a342091996-12-19 23:50:02 +00003490 return;
3491
Barry Warsawca74da41999-02-09 19:31:45 +00003492 PyDict_SetItemString(d, "error", PyExc_OSError);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003493}